From 9314433781c63274efd50ebd0bdd7e1d922e1d76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Wed, 10 Oct 2007 18:33:15 +0000 Subject: [PATCH] Big switch of LB to the new DB module. --- org.glite.lb.common/interface/context-int.h | 7 +- org.glite.lb.server/Makefile | 26 +- org.glite.lb.server/interface/lbs_db.h | 135 -------- org.glite.lb.server/src/bkindex.c | 42 ++- org.glite.lb.server/src/bkserverd.c | 52 +-- org.glite.lb.server/src/db_calls.c | 10 +- org.glite.lb.server/src/db_store.c | 1 - org.glite.lb.server/src/db_supp.c | 79 +++++ org.glite.lb.server/src/db_supp.h | 26 ++ org.glite.lb.server/src/dump.c | 18 +- org.glite.lb.server/src/get_events.c.T | 11 +- org.glite.lb.server/src/get_events.h | 3 - org.glite.lb.server/src/index.c.T | 106 +++--- org.glite.lb.server/src/index_parse.y | 1 - org.glite.lb.server/src/jobstat.c | 64 ++-- org.glite.lb.server/src/jobstat.h | 3 +- org.glite.lb.server/src/jobstat_supp.c | 5 +- org.glite.lb.server/src/lb_authz.c | 26 +- org.glite.lb.server/src/lbs_db.c | 450 ------------------------- org.glite.lb.server/src/lbs_db_supp.c | 32 -- org.glite.lb.server/src/load.c | 2 +- org.glite.lb.server/src/mon-db.c | 17 +- org.glite.lb.server/src/notif_match.c | 12 +- org.glite.lb.server/src/notification.c | 53 +-- org.glite.lb.server/src/openserver.c | 9 +- org.glite.lb.server/src/query.c | 47 +-- org.glite.lb.server/src/request.c | 2 +- org.glite.lb.server/src/seqcode.c | 9 +- org.glite.lb.server/src/server_state.c | 14 +- org.glite.lb.server/src/srv_purge.c | 37 +- org.glite.lb.server/src/store.c.T | 97 +++--- org.glite.lb.server/src/userjobs.c | 17 +- org.glite.lb.server/test/test_query_events.cpp | 58 ++-- org.glite.lbjp-common.db/interface/db.h | 6 + org.glite.lbjp-common.db/src/db.c | 52 +-- 35 files changed, 523 insertions(+), 1006 deletions(-) delete mode 100644 org.glite.lb.server/interface/lbs_db.h create mode 100644 org.glite.lb.server/src/db_supp.c create mode 100644 org.glite.lb.server/src/db_supp.h delete mode 100644 org.glite.lb.server/src/lbs_db.c delete mode 100644 org.glite.lb.server/src/lbs_db_supp.c diff --git a/org.glite.lb.common/interface/context-int.h b/org.glite.lb.common/interface/context-int.h index 53eb3a4..bea1992 100644 --- a/org.glite.lb.common/interface/context-int.h +++ b/org.glite.lb.common/interface/context-int.h @@ -4,6 +4,7 @@ #ident "$Header$" #include "glite/security/glite_gss.h" +#include "glite/lbu/db.h" #include "glite/lb/jobstat.h" #include "glite/lb/query_rec.h" #include "glite/lb/lb_plain_io.h" @@ -49,7 +50,8 @@ struct _edg_wll_Context { /* server part */ - void *mysql; + glite_lbu_DBContext dbctx; + int dbcaps; edg_wll_Connections *connections; edg_wll_ConnPool *connPoolNotif; /* hold _one_ connection from notif-interlogger */ edg_wll_ConnProxy *connProxy; /* holds one plain connection */ @@ -129,9 +131,6 @@ struct _edg_wll_Context { edg_wll_SeqCode p_seqcode; int count_statistics; - /* TODO: belongs to database part */ - int use_transactions; - int greyjobs; }; diff --git a/org.glite.lb.server/Makefile b/org.glite.lb.server/Makefile index 1516418..471e852 100644 --- a/org.glite.lb.server/Makefile +++ b/org.glite.lb.server/Makefile @@ -94,11 +94,13 @@ CFLAGS:= \ ifdef LB_PROF SRVBONES_LIB:= ${stagedir}/lib/libglite_lbu_server_bones.la LB_COMMON_LIB:=${stagedir}/lib/libglite_lb_common_${nothrflavour}.la + LB_UTILS_DB_LIB:=${stagedir}/lib/libglite_lbu_db.la CFLAGS:=${CFLAGS} -pg -g LDFLAGS:=${LDFLAGS} -pg else SRVBONES_LIB:= -L${stagedir}/lib -lglite_lbu_server_bones LB_COMMON_LIB:=-lglite_lb_common_${nothrflavour} + LB_UTILS_DB_LIB:=-lglite_lbu_db LDFLAGS:= -L${stagedir}/lib endif @@ -172,15 +174,15 @@ COMMON_LIBS:= -L${stagedir}/lib -lglite_lb_common_${nothrflavour} -lglite_secur PLUGIN_LIBS:= -L${stagedir}/lib -lglite_lb_common_${nothrflavour} -lglite_lbu_trio \ ${classadslib} -lstdc++ ${expatlib} -lexpat\ -PLUGIN_LOBJS:= lb_plugin.lo jobstat_supp.lo process_event.lo lbs_db_supp.lo process_event_pbs.lo process_event_condor.lo +PLUGIN_LOBJS:= lb_plugin.lo jobstat_supp.lo process_event.lo process_event_pbs.lo process_event_condor.lo BKSERVER_BASE_OBJS:= \ bkserverd.o il_lbproxy.o get_events.o index.o jobstat.o jobstat_supp.o process_event.o process_event_pbs.o process_event_condor.o \ - seqcode.o write2rgma.o lbs_db.o lbs_db_supp.o lb_html.o lb_http.o lb_proto.o lb_xml_parse.o \ + seqcode.o write2rgma.o lb_html.o lb_http.o lb_proto.o lb_xml_parse.o \ lb_xml_parse_V21.o \ lock.o openserver.o query.o userjobs.o db_store.o request.o store.o \ stored_master.o srv_purge.o server_state.o dump.o lb_authz.o load.o \ - notification.o il_notification.o notif_match.o stats.o db_calls.o + notification.o il_notification.o notif_match.o stats.o db_calls.o db_supp.o dotless_gsoap_ver:=${shell echo ${gsoap_version} | tr -d . } GSOAP_LIB:=-L${stagedir}/lib -lglite_security_gsoap_plugin_${dotless_gsoap_ver}_${nothrflavour} @@ -194,6 +196,7 @@ ifeq ($(GLITE_LB_SERVER_WITH_WS),yes) BKSERVER_LIBS= \ ${SRVBONES_LIB} \ ${LB_COMMON_LIB} \ + ${LB_UTILS_DB_LIB} \ ${GSOAP_LIB} \ ${EXT_LIBS} else @@ -202,16 +205,17 @@ else BKSERVER_LIBS= \ ${SRVBONES_LIB} \ ${LB_COMMON_LIB} \ + ${LB_UTILS_DB_LIB} \ -lglite_security_gss_${nothrflavour} \ ${EXT_LIBS} endif -INDEX_OBJS:= index.o index_parse.o jobstat_supp.o lbs_db.o lbs_db_supp.o openserver.o \ +INDEX_OBJS:= index.o index_parse.o jobstat_supp.o openserver.o \ jobstat.o process_event.o process_event_pbs.o process_event_condor.o query.o lock.o get_events.o write2rgma.o index_lex.o \ lb_authz.o store.o bkindex.o stats.o\ - request.o db_store.o srv_purge.o notif_match.o il_lbproxy.o dump.o lb_xml_parse.o il_notification.o lb_proto.o server_state.o lb_xml_parse_V21.o lb_html.o notification.o seqcode.o userjobs.o load.o db_calls.o + request.o db_store.o srv_purge.o notif_match.o il_lbproxy.o dump.o lb_xml_parse.o il_notification.o lb_proto.o server_state.o lb_xml_parse_V21.o lb_html.o notification.o seqcode.o userjobs.o load.o db_calls.o db_supp.lo -INDEX_LIBS:= ${SRVBONES_LIB} ${COMMON_LIBS} ${EXT_LIBS} +INDEX_LIBS:= ${SRVBONES_LIB} ${COMMON_LIBS} ${EXT_LIBS} ${LB_UTILS_DB_LIB} WS_CLIENT_OBJS:= $(GSOAP_FILES_PREFIX)C.o $(GSOAP_FILES_PREFIX)Client.o ws_fault.o ws_typeref.o WS_CLIENT_LIBS:= ${GSOAP_LIB} -lglite_lb_common_${nothrflavour} \ @@ -225,22 +229,22 @@ WS_CLIENT_LIBS:= ${GSOAP_LIB} -lglite_lb_common_${nothrflavour} \ # ${EXT_LIBS} # -HDRS=index.h lb_authz.h lbs_db.h store.h +HDRS=index.h lb_authz.h store.h MAN_GZ:=glite-lb-mon-db.1.gz MAN = $(MAN_GZ:.gz=) LIB_OBJS_BK:= \ il_lbproxy.o get_events.o index.o jobstat.o jobstat_supp.o process_event.o process_event_pbs.o process_event_condor.o \ - seqcode.o lbs_db.o lbs_db_supp.o lb_html.o lb_http.o lb_proto.o lb_xml_parse.o \ + seqcode.o lb_html.o lb_http.o lb_proto.o lb_xml_parse.o \ lb_xml_parse_V21.o \ lock.o openserver.o query.o userjobs.o db_store.o request.o store.o \ stored_master.o srv_purge.o server_state.o dump.o lb_authz.o load.o \ notification.o il_notification.o notif_match.o stats.o write2rgma.o \ - db_calls.o + db_calls.o db_supp.o MONDB_OBJS:=mon-db.o ${LIB_OBJS_BK} -MONDB_LIBS:=${COMMON_LIBS} ${EXT_LIBS} +MONDB_LIBS:=${COMMON_LIBS} ${LB_UTILS_DB_LIB} ${EXT_LIBS} glite-lb-bkserverd: ${NSMAP} ${BKSERVER_OBJS} ${LINKXX} -o $@ ${BKSERVER_OBJS} ${BKSERVER_LIBS} @@ -308,7 +312,7 @@ test.query: test_query_events ./test_query_events #query_events_objs:=`echo ${BKSERVER_BASE_OBJS} |sed 's/lbs_db\.o//;s/lbs_db_supp\.o//;s/bkserverd\.o//;s/db_store\.o//;s/stored_master\.o//;s/store\.o//;s/srv_purge\.o//;s/load\.o//;s/dump\.o//;s/lb_proto\.o//;s/lb_html\.o//;s/lb_http\.o//;'` -query_events_objs:=`echo ${BKSERVER_BASE_OBJS} |sed 's/lbs_db\.o//;s/lbs_db_supp\.o//;s/bkserverd\.o//;'` +query_events_objs:=`echo ${BKSERVER_BASE_OBJS} |sed 's/bkserverd\.o//;'` test_query_events: test_query_events.o ${LINKXX} -o $@ test_query_events.o ${query_events_objs} \ diff --git a/org.glite.lb.server/interface/lbs_db.h b/org.glite.lb.server/interface/lbs_db.h deleted file mode 100644 index 7e212d9..0000000 --- a/org.glite.lb.server/interface/lbs_db.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef GLITE_LB_LBS_DB_H -#define GLITE_LB_LBS_DB_H - -#ident "$Header$" - -#include -#include - -#include "glite/lb/context.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define EDG_WLL_MYSQL_VERSION 40001 -#define BUF_INSERT_ROW_ALLOC_BLOCK 1000 - - -#define DEFAULTCS "lbserver/@localhost:lbserver20" - -typedef struct _edg_wll_Stmt *edg_wll_Stmt; - -edg_wll_ErrorCode edg_wll_DBConnect( - edg_wll_Context, /* INOUT: */ - const char * /* IN: connect string user/password@host:database */ -); - - -/* - * structure holds date for multi-rows insert - */ -typedef struct _edg_wll_bufInsert { - edg_wll_Context ctx; - char *table_name; - char *columns; /* names of columns to be inserted into - * (values separated with commas) */ - char **rows; /* each row hold string of one row to be inserted - * (values separated with commas) */ - long rec_num, /* actual number of rows in structure */ - rec_size; /* approx. size of a real insert string */ - long size_limit, /* size and # of records limit which trigger */ - record_limit; /* real insert; zero means unlimitted */ -} edg_wll_bufInsert; - - -void edg_wll_DBClose(edg_wll_Context); - - -/* Parse and execute SQL statement. Returns number of rows selected, created - * or affected by update, or -1 on error */ - -int edg_wll_ExecStmt( - edg_wll_Context, /* INOUT: */ - char *, /* IN: SQL statement */ - edg_wll_Stmt * /* OUT: statement handle. Usable for select only */ -); - - -/* Fetch next row of select statement. - * All columns are returned as fresh allocated strings - * - * return values: - * >0 - number of fields of the retrieved row - * 0 - no more rows - * -1 - error - * - * Errors are stored in context passed to previous edg_wll_ExecStmt() */ - -int edg_wll_FetchRow( - edg_wll_Stmt, /* IN: statement */ - char ** /* OUT: array of fetched values. - * As number of columns is fixed and known, - * expects allocated array of pointers here */ -); - -/* Retrieve column names of a query statement */ - -int edg_wll_QueryColumns( - edg_wll_Stmt, /* IN: statement */ - char ** /* OUT: result set column names. Expects allocated array. */ -); - -/* Free the statement structure */ - -void edg_wll_FreeStmt( - edg_wll_Stmt * /* INOUT: statement */ -); - - -/** - * convert time_t into database-specific time string - * - * returns pointer to dynamic area which should be freed - */ -char *edg_wll_TimeToDB(time_t); -time_t edg_wll_DBToTime(char *); - -extern edg_wll_ErrorCode edg_wll_Open(edg_wll_Context ctx, char *cs); - -/** - * Check database version. - */ -int edg_wll_DBCheckVersion(edg_wll_Context, const char *); - -int edg_wll_Transaction(edg_wll_Context ctx); -int edg_wll_Commit(edg_wll_Context ctx); -int edg_wll_Rollback(edg_wll_Context ctx); - -/** - * Init data structure for buffered insert - * - * takes table_name and columns string for future multirow insert - * when insert string oversize size_limit or number of rows to be inserted - * overcome record_limit, the real insert is triggered - */ -edg_wll_ErrorCode edg_wll_bufferedInsertInit(edg_wll_Context ctx, edg_wll_bufInsert *bi, void *mysql, char *table_name, long size_limit, long record_limit, char * columns); - -/** - * adds row of n values into n columns into an insert buffer - * if num. of rows or size of data oversteps the limits, real - * multi-row insert is done - */ -edg_wll_ErrorCode edg_wll_bufferedInsert(edg_wll_bufInsert *bi, char *row); - -/** - * flush buffered data and free bi structure - */ -edg_wll_ErrorCode edg_wll_bufferedInsertClose(edg_wll_bufInsert *bi); - - -#ifdef __cplusplus -} -#endif - -#endif /* GLITE_LB_LBS_DB_H */ diff --git a/org.glite.lb.server/src/bkindex.c b/org.glite.lb.server/src/bkindex.c index 47d9fb2..977ef4a 100644 --- a/org.glite.lb.server/src/bkindex.c +++ b/org.glite.lb.server/src/bkindex.c @@ -10,10 +10,11 @@ #include #include "glite/jobid/strmd5.h" +#include "glite/lbu/trio.h" #include "glite/lb/context-int.h" #include "index.h" -#include "lbs_db.h" #include "jobstat.h" +#include "db_supp.h" #ifdef LB_PERF #include "glite/lb/lb_perftest.h" @@ -83,8 +84,14 @@ int main(int argc,char **argv) } edg_wll_InitContext(&ctx); + glite_lbu_InitDBContext(&ctx->dbctx); + if (!dbstring) dbstring = DEFAULTCS; if (edg_wll_Open(ctx,dbstring)) do_exit(ctx,EX_UNAVAILABLE); - if (edg_wll_DBCheckVersion(ctx,dbstring)) do_exit(ctx,EX_SOFTWARE); + if ((ctx->dbcaps = glite_lbu_DBQueryCaps(ctx->dbctx)) == -1) do_exit(ctx, EX_SOFTWARE); + if (!(ctx->dbcaps & GLITE_LBU_DB_CAP_INDEX)) { + edg_wll_SetError(ctx, EDG_WLL_ERROR_DB_CALL, "index capability not available"); + do_exit(ctx, EX_SOFTWARE); + } if (edg_wll_QueryJobIndices(ctx,&old_indices,&index_names)) do_exit(ctx,EX_SOFTWARE); if (dump) { @@ -97,7 +104,7 @@ int main(int argc,char **argv) for (i=0; index_names && index_names[i]; i++) { asprintf(&stmt,"alter table states drop index `%s`",index_names[i]); if (verbose) putchar('.'); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); } if (verbose) puts(" done"); @@ -106,7 +113,7 @@ int main(int argc,char **argv) char *cname = edg_wll_QueryRecToColumn(old_indices[i]); asprintf(&stmt,"alter table states drop column `%s`",cname); if (verbose) putchar('.'); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); free(cname); } @@ -177,7 +184,7 @@ int main(int argc,char **argv) } if (really) { asprintf(&stmt,"alter table states drop index `%s`",index_names[drop_indices[i]]); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); } if (verbose) puts(really ? "done" : ""); @@ -191,7 +198,7 @@ int main(int argc,char **argv) if (verbose) printf("\t%s\n",cname); if (really) { asprintf(&stmt,"alter table states drop column `%s`",cname); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); } free(cname); @@ -209,7 +216,7 @@ int main(int argc,char **argv) if (really) { char *ctype = db_col_type(new_columns[i]); asprintf(&stmt,"alter table states add `%s` %s",cname,ctype); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); } memcpy(&added_icols[nadd_icols].qrec, new_columns[i], sizeof(edg_wll_QueryRec)); @@ -242,12 +249,14 @@ int main(int argc,char **argv) if (really) { asprintf(&stmt,"create index `%s` on states(%s)",n,l); free(n); free(l); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) do_exit(ctx,EX_SOFTWARE); free(stmt); } if (verbose) puts(really ? "done" : ""); } + edg_wll_Close(ctx); + return 0; } @@ -295,6 +304,8 @@ static void do_exit(edg_wll_Context ctx,int code) edg_wll_Error(ctx,&et,&ed); fprintf(stderr,"edg-bkindex: %s (%s)\n",et,ed); + edg_wll_Close(ctx); + edg_wll_FreeContext(ctx); exit(code); } @@ -318,7 +329,7 @@ static void usage(const char *me) edg_wll_ErrorCode edg_wll_RefreshIColumns(edg_wll_Context ctx, void *job_index_cols) { - edg_wll_Stmt sh, sh2; + glite_lbu_Statement sh; int njobs, ret = -1; intJobStat *stat; edg_wlc_JobId jobid; @@ -330,12 +341,12 @@ edg_wll_ErrorCode edg_wll_RefreshIColumns(edg_wll_Context ctx, void *job_index_c edg_wll_ResetError(ctx); if (!job_index_cols) return 0; - if ((njobs = edg_wll_ExecStmt(ctx, "select s.jobid,s.int_status,s.seq,s.version,j.dg_jobid" + if ((njobs = edg_wll_ExecSQL(ctx, "select s.jobid,s.int_status,s.seq,s.version,j.dg_jobid" " from states s, jobs j where s.jobid=j.jobid",&sh)) < 0) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); return edg_wll_Error(ctx, NULL, NULL); } - while ((ret=edg_wll_FetchRow(sh,res)) >0) { + while ((ret=edg_wll_FetchRow(ctx,sh,sizeof(res)/sizeof(res[0]),NULL,res)) >0) { if (strcmp(res[3], INTSTAT_VERSION)) { stat = NULL; if (!edg_wlc_JobIdParse(res[4], &jobid)) { @@ -352,15 +363,14 @@ edg_wll_ErrorCode edg_wll_RefreshIColumns(edg_wll_Context ctx, void *job_index_c if (rest == NULL) stat = NULL; } if (stat == NULL) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); return edg_wll_SetError(ctx, EDG_WLL_ERROR_SERVER_RESPONSE, "cannot decode int_status from states DB table"); } edg_wll_IColumnsSQLPart(ctx, job_index_cols, stat, 0, NULL, &icvalues); trio_asprintf(&stmt, "update states set seq=%s%s where jobid='%|Ss'", res[2], icvalues, res[0]); - ret = edg_wll_ExecStmt(ctx, stmt, &sh2); - edg_wll_FreeStmt(&sh2); + ret = edg_wll_ExecSQL(ctx, stmt, NULL); for (i = 0; i < 5; i++) free(res[i]); destroy_intJobStat(stat); free(stat); @@ -369,7 +379,7 @@ edg_wll_ErrorCode edg_wll_RefreshIColumns(edg_wll_Context ctx, void *job_index_c if (ret < 0) return edg_wll_Error(ctx, NULL, NULL); } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); return edg_wll_Error(ctx, NULL, NULL); } diff --git a/org.glite.lb.server/src/bkserverd.c b/org.glite.lb.server/src/bkserverd.c index 914cb81..58bf47f 100644 --- a/org.glite.lb.server/src/bkserverd.c +++ b/org.glite.lb.server/src/bkserverd.c @@ -53,11 +53,11 @@ enum lb_srv_perf_sink sink_mode; #include "lb_http.h" #include "lb_proto.h" #include "index.h" -#include "lbs_db.h" #include "lb_authz.h" #include "il_notification.h" #include "stats.h" #include "db_calls.h" +#include "db_supp.h" #ifdef GLITE_LB_SERVER_WITH_WS # if GSOAP_VERSION < 20700 @@ -137,7 +137,8 @@ static int hardJobsLimit = 0; static int hardEventsLimit = 0; static int hardRespSizeLimit = 0; static char *dbstring = NULL,*fake_host = NULL; -int transactions = -1, use_transactions = -1; +int transactions = -1; +int use_dbcaps = 0; static int fake_port = 0; static char **super_users = NULL; static int slaves = 10, @@ -353,8 +354,8 @@ struct clnt_data_t { #ifdef GLITE_LB_SERVER_WITH_WS struct soap *soap; #endif /* GLITE_LB_SERVER_WITH_WS */ - int use_transactions; - void *mysql; + glite_lbu_DBContext dbctx; + int dbcaps; edg_wll_QueryRec **job_index; edg_wll_IColumnRec *job_index_cols; int mode; @@ -708,27 +709,36 @@ int main(int argc, char *argv[]) /* Just check the database and let it be. The slaves do the job. */ edg_wll_InitContext(&ctx); + glite_lbu_InitDBContext(&ctx->dbctx); wait_for_open(ctx, dbstring); - if (edg_wll_DBCheckVersion(ctx, dbstring)) + if ((ctx->dbcaps = glite_lbu_DBQueryCaps(ctx->dbctx)) == -1) { char *et,*ed; - edg_wll_Error(ctx,&et,&ed); + glite_lbu_DBError(ctx->dbctx,&et,&ed); fprintf(stderr,"%s: open database: %s (%s)\n",argv[0],et,ed); + free(et); free(ed); return 1; } + edg_wll_Close(ctx); + ctx->dbctx = NULL; fprintf(stderr, "[%d]: DB '%s'\n", getpid(), dbstring); - if (count_statistics) edg_wll_InitStatistics(ctx); - if (!ctx->use_transactions && transactions != 0) { - fprintf(stderr, "[%d]: transactions aren't supported!\n", getpid()); + + if ((ctx->dbcaps & GLITE_LBU_DB_CAP_INDEX) == 0) { + fprintf(stderr,"%s: missing index support in DB layer\n",argv[0]); + return 1; } + if ((ctx->dbcaps & GLITE_LBU_DB_CAP_TRANSACTIONS) == 0) + fprintf(stderr, "[%d]: transactions aren't supported!\n", getpid()); if (transactions >= 0) { - fprintf(stderr, "[%d]: transactions forced from %d to %d\n", getpid(), ctx->use_transactions, transactions); - ctx->use_transactions = transactions; + fprintf(stderr, "[%d]: transactions forced from %d to %d\n", getpid(), ctx->dbcaps & GLITE_LBU_DB_CAP_TRANSACTIONS ? 1 : 0, transactions); + ctx->dbcaps &= ~GLITE_LBU_DB_CAP_TRANSACTIONS; + ctx->dbcaps |= transactions ? GLITE_LBU_DB_CAP_TRANSACTIONS : 0; } - use_transactions = ctx->use_transactions; - edg_wll_Close(ctx); + use_dbcaps = ctx->dbcaps; + + if (count_statistics) edg_wll_InitStatistics(ctx); edg_wll_FreeContext(ctx); if ( !debug ) { @@ -826,8 +836,9 @@ int bk_clnt_data_init(void **data) dprintf(("[%d] opening database ...\n", getpid())); wait_for_open(ctx, dbstring); - cdata->mysql = ctx->mysql; - cdata->use_transactions = ctx->use_transactions; + glite_lbu_DBSetCaps(ctx->dbctx, use_dbcaps); + cdata->dbctx = ctx->dbctx; + cdata->dbcaps = use_dbcaps; if ( edg_wll_QueryJobIndices(ctx, &job_index, NULL) ) { @@ -938,8 +949,8 @@ int bk_handle_connection(int conn, struct timeval *timeout, void *data) */ ctx->serverRunning = cdata->mode & SERVICE_SERVER; ctx->proxyRunning = cdata->mode & SERVICE_PROXY; - ctx->mysql = cdata->mysql; - ctx->use_transactions = cdata->use_transactions; + ctx->dbctx = cdata->dbctx; + ctx->dbcaps = cdata->dbcaps; ctx->job_index_cols = cdata->job_index_cols; ctx->job_index = cdata->job_index; @@ -1192,7 +1203,8 @@ int bk_handle_connection_proxy(int conn, struct timeval *timeout, void *data) */ ctx->serverRunning = cdata->mode & SERVICE_SERVER; ctx->proxyRunning = cdata->mode & SERVICE_PROXY; - ctx->mysql = cdata->mysql; + ctx->dbctx = cdata->dbctx; + ctx->dbcaps = cdata->dbcaps; /* set globals */ @@ -1521,7 +1533,7 @@ static void wait_for_open(edg_wll_Context ctx, const char *dbstring) char *errt,*errd; if (dbfail_string1) free(dbfail_string1); - edg_wll_Error(ctx,&errt,&errd); + glite_lbu_DBError(ctx->dbctx,&errt,&errd); asprintf(&dbfail_string1,"%s (%s)\n",errt,errd); if (dbfail_string1 != NULL) { if (dbfail_string2 == NULL || strcmp(dbfail_string1,dbfail_string2)) { @@ -1541,8 +1553,6 @@ static void wait_for_open(edg_wll_Context ctx, const char *dbstring) dprintf(("[%d]: DB connection established\n",getpid())); if (!debug) syslog(LOG_INFO,"DB connection established\n"); } - - ctx->use_transactions = use_transactions; } static void free_hostent(struct hostent *h){ diff --git a/org.glite.lb.server/src/db_calls.c b/org.glite.lb.server/src/db_calls.c index 933964a..941abdb 100644 --- a/org.glite.lb.server/src/db_calls.c +++ b/org.glite.lb.server/src/db_calls.c @@ -8,8 +8,8 @@ #include "glite/lb/context-int.h" -#include "lbs_db.h" #include "db_calls.h" +# include "db_supp.h" /** Returns bitmask of job membership in common server/proxy database */ @@ -17,7 +17,7 @@ int edg_wll_jobMembership(edg_wll_Context ctx, edg_wlc_JobId job) { char *dbjob; char *stmt = NULL; - edg_wll_Stmt q; + glite_lbu_Statement q; int ret, result = -1; char *res[2] = { NULL, NULL}; @@ -26,7 +26,7 @@ int edg_wll_jobMembership(edg_wll_Context ctx, edg_wlc_JobId job) dbjob = edg_wlc_JobIdGetUnique(job); trio_asprintf(&stmt,"select proxy,server from jobs where jobid = '%|Ss'",dbjob); - ret = edg_wll_ExecStmt(ctx,stmt,&q); + ret = edg_wll_ExecSQL(ctx,stmt,&q); if (ret <= 0) { if (ret == 0) { fprintf(stderr,"%s: no such job\n",dbjob); @@ -36,7 +36,7 @@ int edg_wll_jobMembership(edg_wll_Context ctx, edg_wlc_JobId job) } free(stmt); stmt = NULL; - if ((ret = edg_wll_FetchRow(q,res)) > 0) { + if ((ret = edg_wll_FetchRow(ctx,q,sizeof(res)/sizeof(res[0]),NULL,res)) > 0) { result = 0; if (strcmp(res[0],"0")) result += DB_PROXY_JOB; if (strcmp(res[1],"0")) result += DB_SERVER_JOB; @@ -48,7 +48,7 @@ int edg_wll_jobMembership(edg_wll_Context ctx, edg_wlc_JobId job) edg_wll_SetError(ctx,ENOENT,dbjob); } } - edg_wll_FreeStmt(&q); + glite_lbu_FreeStmt(&q); clean: free(res[0]); free(res[1]); diff --git a/org.glite.lb.server/src/db_store.c b/org.glite.lb.server/src/db_store.c index b2fdafb..9fd728a 100644 --- a/org.glite.lb.server/src/db_store.c +++ b/org.glite.lb.server/src/db_store.c @@ -12,7 +12,6 @@ #include "glite/lb/lb_maildir.h" #include "purge.h" #include "store.h" -#include "lbs_db.h" #include "lock.h" #include "il_lbproxy.h" #include "jobstat.h" diff --git a/org.glite.lb.server/src/db_supp.c b/org.glite.lb.server/src/db_supp.c new file mode 100644 index 0000000..692d833 --- /dev/null +++ b/org.glite.lb.server/src/db_supp.c @@ -0,0 +1,79 @@ +#include +#include + +#include "glite/lbu/db.h" +#include "glite/lb/context-int.h" + +int edg_wll_SetErrorDB(edg_wll_Context ctx) { + int code; + char *ed; + + if (ctx->dbctx) { + code = glite_lbu_DBError(ctx->dbctx, NULL, &ed); + edg_wll_SetError(ctx, code, ed); + free(ed); + } else { + code = EINVAL; + edg_wll_SetError(ctx, EINVAL, "DB context isn't created"); + } + + return code; +} + + +int edg_wll_ExecSQL(edg_wll_Context ctx, const char *cmd, glite_lbu_Statement *stmt) { + int retval; + + if ((retval = glite_lbu_ExecSQL(ctx->dbctx, cmd, stmt)) < 0) edg_wll_SetErrorDB(ctx); + return retval; +} + + +int edg_wll_FetchRow(edg_wll_Context ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results) { + int retval; + + if ((retval = glite_lbu_FetchRow(stmt, n, lengths, results)) < 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_bufferedInsertInit(edg_wll_Context ctx, glite_lbu_bufInsert *bi, const char *table_name, long size_limit, long record_limit, const char *columns) { + int retval; + + if ((retval = glite_lbu_bufferedInsertInit(ctx->dbctx, bi, table_name, size_limit, record_limit, columns)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_bufferedInsert(edg_wll_Context ctx, glite_lbu_bufInsert bi, const char *row) { + int retval; + + if ((retval = glite_lbu_bufferedInsert(bi, row)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_bufferedInsertClose(edg_wll_Context ctx, glite_lbu_bufInsert bi) { + int retval; + + if ((retval = glite_lbu_bufferedInsertClose(bi)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_Transaction(edg_wll_Context ctx) { + int retval; + + if ((retval = glite_lbu_Transaction(ctx->dbctx)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_Commit(edg_wll_Context ctx) { + int retval; + + if ((retval = glite_lbu_Commit(ctx->dbctx)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} + +int edg_wll_Rollback(edg_wll_Context ctx) { + int retval; + + if ((retval = glite_lbu_Rollback(ctx->dbctx)) != 0) edg_wll_SetErrorDB(ctx); + return retval; +} diff --git a/org.glite.lb.server/src/db_supp.h b/org.glite.lb.server/src/db_supp.h new file mode 100644 index 0000000..f53b817 --- /dev/null +++ b/org.glite.lb.server/src/db_supp.h @@ -0,0 +1,26 @@ +#ifndef EDG_WLL_DB_SUPP_H +#define EDG_WLL_DB_SUPP_H + +#ident "$Header:" + +#include "glite/lbu/db.h" + +#define DEFAULTCS "lbserver/@localhost:lbserver20" + +/** + * Set the current database error. + */ +int edg_wll_SetErrorDB(edg_wll_Context ctx); + +int edg_wll_ExecSQL(edg_wll_Context ctx, const char *cmd, glite_lbu_Statement *stmt); +int edg_wll_FetchRow(edg_wll_Context ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results); + +int edg_wll_bufferedInsertInit(edg_wll_Context ctx, glite_lbu_bufInsert *bi, const char *table_name, long size_limit, long record_limit, const char *columns); +int edg_wll_bufferedInsert(edg_wll_Context ctx, glite_lbu_bufInsert bi, const char *row); +int edg_wll_bufferedInsertClose(edg_wll_Context ctx, glite_lbu_bufInsert bi); + +int edg_wll_Transaction(edg_wll_Context ctx); +int edg_wll_Commit(edg_wll_Context ctx); +int edg_wll_Rollback(edg_wll_Context ctx); + +#endif diff --git a/org.glite.lb.server/src/dump.c b/org.glite.lb.server/src/dump.c index cc7063e..1b9df12 100644 --- a/org.glite.lb.server/src/dump.c +++ b/org.glite.lb.server/src/dump.c @@ -15,11 +15,11 @@ #include "glite/lb/events_parse.h" #include "glite/lb/ulm_parse.h" -#include "lbs_db.h" #include "query.h" #include "get_events.h" #include "server_state.h" #include "purge.h" +#include "db_supp.h" static char *time_to_string(time_t t, char **ptr); static int handle_specials(edg_wll_Context,time_t *); @@ -31,7 +31,7 @@ int edg_wll_DumpEventsServer(edg_wll_Context ctx,const edg_wll_DumpRequest *req, char *from_s, *to_s, *stmt, *time_s, *ptr; char *tmpfname; time_t start,end; - edg_wll_Stmt q = NULL; + glite_lbu_Statement q = NULL; char *res[10]; int event; edg_wll_Event e; @@ -54,8 +54,8 @@ int edg_wll_DumpEventsServer(edg_wll_Context ctx,const edg_wll_DumpRequest *req, return edg_wll_Error(ctx,NULL,NULL); } - from_s = strdup(edg_wll_TimeToDB(from)); - to_s = strdup(edg_wll_TimeToDB(to)); + glite_lbu_TimeToDB(from, &from_s); + glite_lbu_TimeToDB(to, &to_s); trio_asprintf(&stmt, "select event,dg_jobid,code,prog,host,u.cert_subj,time_stamp,usec,level,arrived " @@ -68,9 +68,9 @@ int edg_wll_DumpEventsServer(edg_wll_Context ctx,const edg_wll_DumpRequest *req, ctx->srvName,ctx->srvPort, from_s,to_s); - if (edg_wll_ExecStmt(ctx,stmt,&q) < 0) goto clean; + if (edg_wll_ExecSQL(ctx,stmt,&q) < 0) goto clean; - while ((ret = edg_wll_FetchRow(q,res)) > 0) { + while ((ret = edg_wll_FetchRow(ctx,q,sizeof(res)/sizeof(res[0]),NULL,res)) > 0) { assert(ret == sizofa(res)); event = atoi(res[0]); free(res[0]); res[0] = NULL; @@ -145,7 +145,7 @@ int edg_wll_DumpEventsServer(edg_wll_Context ctx,const edg_wll_DumpRequest *req, clean: edg_wll_FreeEvent(&e); - edg_wll_FreeStmt(&q); + glite_lbu_FreeStmt(&q); free(stmt); free(from_s); @@ -174,7 +174,7 @@ static int handle_specials(edg_wll_Context ctx,time_t *t) case ENOENT: *t = 0; edg_wll_ResetError(ctx); break; - case 0: *t = edg_wll_DBToTime(time_s); + case 0: *t = glite_lbu_DBToTime(time_s); assert(*t >= 0); break; default: break; @@ -189,7 +189,7 @@ static int handle_specials(edg_wll_Context ctx,time_t *t) static char *time_to_string(time_t t, char **ptr) { char *s; - s = edg_wll_TimeToDB(t); + glite_lbu_TimeToDB(t, &s); s[strlen(s) - 1] = '\0'; *ptr = s; diff --git a/org.glite.lb.server/src/get_events.c.T b/org.glite.lb.server/src/get_events.c.T index 00df56c..4e0bfbe 100644 --- a/org.glite.lb.server/src/get_events.c.T +++ b/org.glite.lb.server/src/get_events.c.T @@ -21,6 +21,7 @@ #include "get_events.h" #include "glite/lb/context-int.h" #include "glite/lb/events_parse.h" +#include "db_supp.h" static void edg_wll_set_event_field(edg_wll_Event *,char *,char *); static void edg_wll_set_event_field_warn(edg_wll_Event *,char *,char *); @@ -29,7 +30,7 @@ int edg_wll_get_event_flesh(edg_wll_Context ctx,int n,edg_wll_Event *e) { char *jobid = edg_wlc_JobIdGetUnique(e->any.jobId), *q = NULL,*nameval[2]; - edg_wll_Stmt sh; + glite_lbu_Statement sh; int ret,t; const char *tables[] = { "short_fields","long_fields" }; @@ -40,9 +41,9 @@ int edg_wll_get_event_flesh(edg_wll_Context ctx,int n,edg_wll_Event *e) "where jobid = '%|Ss' and event = %d ", tables[t],jobid,n); - if ((ret=edg_wll_ExecStmt(ctx,q,&sh)) < 0) goto cleanup; + if ((ret=edg_wll_ExecSQL(ctx,q,&sh)) < 0) goto cleanup; - while ((ret=edg_wll_FetchRow(sh,nameval)) > 0) { + while ((ret=edg_wll_FetchRow(ctx,sh,sizeof(nameval)/sizeof(nameval[0]),NULL,nameval)) > 0) { edg_wll_set_event_field(e,nameval[0],nameval[1]); free(nameval[0]); /* XXX: nameval[1] freed in edg_wll_set_event_field @@ -51,14 +52,14 @@ int edg_wll_get_event_flesh(edg_wll_Context ctx,int n,edg_wll_Event *e) } if (ret<0) goto cleanup; - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); free(q); q=NULL; } ret=edg_wll_CheckEvent(ctx,e); cleanup: - if (sh) edg_wll_FreeStmt(&sh); + if (sh) glite_lbu_FreeStmt(&sh); free(jobid); free(q); diff --git a/org.glite.lb.server/src/get_events.h b/org.glite.lb.server/src/get_events.h index af60075..a45ac7f 100644 --- a/org.glite.lb.server/src/get_events.h +++ b/org.glite.lb.server/src/get_events.h @@ -6,9 +6,6 @@ #include "glite/lb/events.h" #include "glite/lb/query_rec.h" -/* Internal functions for getting event sets from the LB database */ -#include "lbs_db.h" - #ifdef __cplusplus extern "C" { #endif diff --git a/org.glite.lb.server/src/index.c.T b/org.glite.lb.server/src/index.c.T index 4f50a1c..0705224 100644 --- a/org.glite.lb.server/src/index.c.T +++ b/org.glite.lb.server/src/index.c.T @@ -4,8 +4,8 @@ #include #include +#include "glite/lbu/db.h" #include "glite/lb/context-int.h" -#include "lbs_db.h" #include "index.h" extern int debug; @@ -20,9 +20,6 @@ extern int debug; int edg_wll_QueryJobIndices(edg_wll_Context ctx,edg_wll_QueryRec *** index_out,char ***keys_out) { edg_wll_QueryRec **idx = NULL; - edg_wll_Stmt stmt = NULL; - - int i,j,ret; static const char *built_in_indices[] = { "PRIMARY", @@ -30,85 +27,60 @@ int edg_wll_QueryJobIndices(edg_wll_Context ctx,edg_wll_QueryRec *** index_out,c NULL }; -/* XXX: "show index from" columns. Matches at least MySQL 4.0.11 */ - - char *showcol[12]; - int Key_name,Seq_in_index,Column_name,Sub_part; - char **keys = NULL; int *cols = NULL; - char **col_names = NULL; + char ***column_names = NULL; + int i, j; int nkeys = 0; - Key_name = Seq_in_index = Column_name = Sub_part = -1; - - if (edg_wll_ExecStmt(ctx,"show index from states",&stmt)<0) - return edg_wll_Error(ctx,NULL,NULL); - while ((ret = edg_wll_FetchRow(stmt,showcol))) { - if (ret < 0) return edg_wll_Error(ctx,NULL,NULL); - assert(ret <= sizeof showcol/sizeof showcol[0]); - - if (!col_names) { - col_names = malloc(ret * sizeof col_names[0]); - edg_wll_QueryColumns(stmt,col_names); - for (i=0; i= 0 && Seq_in_index >= 0 && - Column_name >= 0 && Sub_part >= 0); + if (glite_lbu_QueryIndices(ctx->dbctx, "states", &keys, &column_names) != 0) { + edg_wll_SetErrorDB(ctx); + return -1; + } + for (i = 0; keys[i]; i++) { + // skip builtin indices + for (j = 0; built_in_indices[j]; j++) { + if (strcasecmp(keys[i], built_in_indices[j]) == 0) + goto next_index; } + cols = realloc(cols,(nkeys+1) * sizeof cols[0]); + cols[nkeys] = 0; + idx = realloc(idx,(nkeys+2) * sizeof idx[0]); + idx[nkeys] = idx[nkeys+1] = NULL; - for (j=0; built_in_indices[j]; j++) { - if (strcasecmp(showcol[Key_name],built_in_indices[j]) == 0) { - for (i=0; i=0 ) { i = 0; stat->children_states = calloc(num_sub+1, sizeof(edg_wll_JobStat)); if (stat->children_states == NULL) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); goto dag_enomem; } - while ((num_f = edg_wll_FetchRow(sh, &stat_str)) == 1 + while ((num_f = edg_wll_FetchRow(ctx, sh, 1, NULL, &stat_str)) == 1 && i < num_sub) { js = dec_intJobStat(stat_str, &s_out); if (s_out != NULL && js != NULL) { @@ -214,7 +214,7 @@ int edg_wll_JobStatus( } free(stat_str); } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); } free(stmt); } else goto dag_enomem; @@ -246,15 +246,15 @@ int edg_wll_JobStatus( md5_jobid, INTSTAT_VERSION); out[1] = NULL; if (stmt != NULL) { - num_sub = edg_wll_ExecStmt(ctx, stmt, &sh); + num_sub = edg_wll_ExecSQL(ctx, stmt, &sh); if (num_sub >=0 ) { - while ((num_f = edg_wll_FetchRow(sh, out)) == 1 ) { + while ((num_f = edg_wll_FetchRow(ctx, sh, sizeof(out)/sizeof(out[0]), NULL, out)) == 1 ) { num_f = atoi(out[0]); if (num_f > EDG_WLL_JOB_UNDEF && num_f < EDG_WLL_NUMBER_OF_STATCODES) stat->children_hist[num_f+1]++; free(out[0]); } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); } free(stmt); } else goto dag_enomem; @@ -285,13 +285,13 @@ int edg_wll_JobStatus( "WHERE s.parent_job='%|Ss' AND s.version='%|Ss' AND s.jobid=j.jobid", md5_jobid, INTSTAT_VERSION); if (stmt != NULL) { - num_sub = edg_wll_ExecStmt(ctx, stmt, &sh); + num_sub = edg_wll_ExecSQL(ctx, stmt, &sh); if (num_sub >=0 ) { - while (edg_wll_FetchRow(sh, out) == 1 ) { + while (edg_wll_FetchRow(ctx, sh, sizeof(out)/sizeof(out[0]), NULL, out) == 1 ) { add_stringlist(&stat->children, out[0]); free(out[0]); } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); } free(stmt); } else goto dag_enomem; @@ -442,7 +442,7 @@ static void warn(const char* format, ...) static char *job_owner(edg_wll_Context ctx,char *md5_jobid) { char *stmt = NULL,*out = NULL; - edg_wll_Stmt sh; + glite_lbu_Statement sh; int f = -1; edg_wll_ResetError(ctx); @@ -454,15 +454,15 @@ static char *job_owner(edg_wll_Context ctx,char *md5_jobid) edg_wll_SetError(ctx,ENOMEM, NULL); return NULL; } - if (edg_wll_ExecStmt(ctx,stmt,&sh) >= 0) { - f=edg_wll_FetchRow(sh,&out); + if (edg_wll_ExecSQL(ctx,stmt,&sh) >= 0) { + f=edg_wll_FetchRow(ctx,sh,1,NULL,&out); if (f == 0) { if (out) free(out); out = NULL; edg_wll_SetError(ctx,ENOENT,md5_jobid); } } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); free(stmt); return out; @@ -550,7 +550,7 @@ edg_wll_ErrorCode edg_wll_StoreIntState(edg_wll_Context ctx, "(jobid,seq,name,value) values " "('%|Ss',%d,'%|Ss','%|Ss')", jobid_md5, seq, (*tagp).tag, (*tagp).value); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { if (EEXIST == edg_wll_Error(ctx, NULL, NULL)) { /* XXX: this should not happen */ edg_wll_ResetError(ctx); @@ -580,7 +580,7 @@ edg_wll_ErrorCode edg_wll_StoreIntState(edg_wll_Context ctx, jobid_md5); free(icvalues); - if ((dbret = edg_wll_ExecStmt(ctx,stmt,NULL)) < 0) goto cleanup; + if ((dbret = edg_wll_ExecSQL(ctx,stmt,NULL)) < 0) goto cleanup; if (dbret == 0) { edg_wll_IColumnsSQLPart(ctx, ctx->job_index_cols, stat, 1, &icnames, &icvalues); @@ -594,19 +594,19 @@ edg_wll_ErrorCode edg_wll_StoreIntState(edg_wll_Context ctx, INTSTAT_VERSION, parent_md5, icvalues); free(icnames); free(icvalues); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) goto cleanup; + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) goto cleanup; } if (update) { trio_asprintf(&stmt, "delete from states " "where jobid ='%|Ss' and ( seq<%d or version !='%|Ss')", jobid_md5, seq, INTSTAT_VERSION); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) goto cleanup; + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) goto cleanup; } if (update) { trio_asprintf(&stmt, "delete from status_tags " "where jobid ='%|Ss' and seq<%d", jobid_md5, seq); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) goto cleanup; + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) goto cleanup; } cleanup: @@ -621,7 +621,7 @@ edg_wll_ErrorCode edg_wll_StoreIntStateEmbryonic(edg_wll_Context ctx, edg_wlc_JobId jobid, char *icnames, char *values, - edg_wll_bufInsert *bi) + glite_lbu_bufInsert *bi) { char *stmt = NULL; @@ -642,7 +642,7 @@ edg_wll_ErrorCode edg_wll_StoreIntStateEmbryonic(edg_wll_Context ctx, "values (%s)", icnames, values); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) goto cleanup; + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) goto cleanup; #endif cleanup: @@ -663,7 +663,7 @@ edg_wll_ErrorCode edg_wll_LoadIntState(edg_wll_Context ctx, { char *jobid_md5; char *stmt; - edg_wll_Stmt sh; + glite_lbu_Statement sh; char *res, *res_rest; int nstates; @@ -687,12 +687,12 @@ edg_wll_ErrorCode edg_wll_LoadIntState(edg_wll_Context ctx, return edg_wll_SetError(ctx, ENOMEM, NULL); } - if ((nstates = edg_wll_ExecStmt(ctx,stmt,&sh)) < 0) goto cleanup; + if ((nstates = edg_wll_ExecSQL(ctx,stmt,&sh)) < 0) goto cleanup; if (nstates == 0) { edg_wll_SetError(ctx,ENOENT,"no state in DB"); goto cleanup; } - if (edg_wll_FetchRow(sh,&res) < 0) goto cleanup; + if (edg_wll_FetchRow(ctx,sh,1,NULL,&res) < 0) goto cleanup; *stat = dec_intJobStat(res, &res_rest); if (res_rest == NULL) { @@ -703,7 +703,7 @@ edg_wll_ErrorCode edg_wll_LoadIntState(edg_wll_Context ctx, free(res); cleanup: free(jobid_md5); - free(stmt); edg_wll_FreeStmt(&sh); + free(stmt); glite_lbu_FreeStmt(&sh); return edg_wll_Error(ctx,NULL,NULL); } @@ -1069,7 +1069,7 @@ edg_wll_ErrorCode edg_wll_GetSubjobHistogram(edg_wll_Context ctx, edg_wlc_JobId { char *stmt = NULL,*out = NULL, *rest = NULL; - edg_wll_Stmt sh; + glite_lbu_Statement sh; int f = -1, i; char *jobid_md5; intJobStat *ijs = NULL; @@ -1085,8 +1085,8 @@ edg_wll_ErrorCode edg_wll_GetSubjobHistogram(edg_wll_Context ctx, edg_wlc_JobId return edg_wll_SetError(ctx,ENOMEM, NULL); } - if (edg_wll_ExecStmt(ctx,stmt,&sh) >= 0) { - f=edg_wll_FetchRow(sh,&out); + if (edg_wll_ExecSQL(ctx,stmt,&sh) >= 0) { + f=edg_wll_FetchRow(ctx,sh,1,NULL,&out); if (f == 0) { if (out) free(out); out = NULL; @@ -1099,7 +1099,7 @@ edg_wll_ErrorCode edg_wll_GetSubjobHistogram(edg_wll_Context ctx, edg_wlc_JobId for (i=0;i<=EDG_WLL_NUMBER_OF_STATCODES;i++) hist[i] = ijs->pub.children_hist[i]; } } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); free(stmt); if (rest==NULL) free(rest); if (ijs==NULL) free(rest); @@ -1136,7 +1136,7 @@ edg_wll_ErrorCode edg_wll_StoreSubjobHistogram(edg_wll_Context ctx, edg_wlc_JobI //printf ("\n\n\n Would like to run SQL statament: %s\n\n\n\n", stmt); - if ((dbret = edg_wll_ExecStmt(ctx,stmt,NULL)) < 0) goto cleanup; + if ((dbret = edg_wll_ExecSQL(ctx,stmt,NULL)) < 0) goto cleanup; assert(dbret); /* update should come through OK as the record exists */ diff --git a/org.glite.lb.server/src/jobstat.h b/org.glite.lb.server/src/jobstat.h index b0bd9e3..b9926f9 100644 --- a/org.glite.lb.server/src/jobstat.h +++ b/org.glite.lb.server/src/jobstat.h @@ -4,7 +4,6 @@ #ident "$Header$" #include "glite/lb/jobstat.h" -#include "lbs_db.h" /* * Internal representation of job state @@ -91,7 +90,7 @@ edg_wll_ErrorCode edg_wll_IColumnsSQLPart(edg_wll_Context, void *, intJobStat *, edg_wll_ErrorCode edg_wll_RefreshIColumns(edg_wll_Context, void *); int edg_wll_intJobStatus( edg_wll_Context, const edg_wlc_JobId, int, intJobStat *, int); edg_wll_ErrorCode edg_wll_StoreIntState(edg_wll_Context, intJobStat *, int); -edg_wll_ErrorCode edg_wll_StoreIntStateEmbryonic(edg_wll_Context, edg_wlc_JobId, char *icnames, char *values, edg_wll_bufInsert *bi); +edg_wll_ErrorCode edg_wll_StoreIntStateEmbryonic(edg_wll_Context, edg_wlc_JobId, char *icnames, char *values, glite_lbu_bufInsert *bi); edg_wll_ErrorCode edg_wll_LoadIntState(edg_wll_Context , edg_wlc_JobId , int, intJobStat **); edg_wll_ErrorCode edg_wll_StepIntStateParent(edg_wll_Context,edg_wlc_JobId,edg_wll_Event *,int,intJobStat *,edg_wll_JobStat *); diff --git a/org.glite.lb.server/src/jobstat_supp.c b/org.glite.lb.server/src/jobstat_supp.c index 18d88df..3411c00 100644 --- a/org.glite.lb.server/src/jobstat_supp.c +++ b/org.glite.lb.server/src/jobstat_supp.c @@ -12,11 +12,11 @@ #include "glite/jobid/cjobid.h" #include "glite/lbu/trio.h" +#include "glite/lbu/db.h" #include "glite/lb/context-int.h" #include "store.h" #include "index.h" -#include "lbs_db.h" #include "jobstat.h" #include "get_events.h" @@ -701,8 +701,7 @@ edg_wll_ErrorCode edg_wll_IColumnsSQLPart(edg_wll_Context ctx, break; case EDG_WLL_QUERY_ATTR_TIME: if (stat->pub.stateEnterTimes) - data = strdup(edg_wll_TimeToDB(stat->pub.stateEnterTimes[ - job_index_cols[i].qrec.attr_id.state+1])); + glite_lbu_TimeToDB(stat->pub.stateEnterTimes[job_index_cols[i].qrec.attr_id.state+1], &data); else data = strdup("0"); break; case EDG_WLL_QUERY_ATTR_RESUBMITTED: diff --git a/org.glite.lb.server/src/lb_authz.c b/org.glite.lb.server/src/lb_authz.c index 786d922..5b1f742 100644 --- a/org.glite.lb.server/src/lb_authz.c +++ b/org.glite.lb.server/src/lb_authz.c @@ -17,7 +17,7 @@ #include "glite/jobid/strmd5.h" #include "glite/jobid/cjobid.h" #include "glite/lbu/trio.h" -#include "lbs_db.h" +#include "db_supp.h" /* XXX should be defined in gridsite-gacl.h */ GRSTgaclEntry *GACLparseEntry(xmlNodePtr cur); @@ -565,7 +565,7 @@ edg_wll_HandleCounterACL(edg_wll_Context ctx, edg_wll_Acl acl, for ( ; ; ) { - if ( edg_wll_ExecStmt(ctx, q1, NULL) > 0 ) + if ( edg_wll_ExecSQL(ctx, q1, NULL) > 0 ) goto end; if ( edg_wll_Error(ctx,NULL,NULL) != EEXIST ) @@ -578,7 +578,7 @@ edg_wll_HandleCounterACL(edg_wll_Context ctx, edg_wll_Acl acl, "update acls set refcnt = refcnt+%d " "where aclid = '%|Ss'", incr, aclid); - if ( edg_wll_ExecStmt(ctx, q2, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q2, NULL) < 0 ) continue; goto end; @@ -591,13 +591,13 @@ edg_wll_HandleCounterACL(edg_wll_Context ctx, edg_wll_Acl acl, "where aclid='%|Ss' and refcnt>=%d", -incr, aclid, -incr); - if ( edg_wll_ExecStmt(ctx, q1, NULL) > 0 ) + if ( edg_wll_ExecSQL(ctx, q1, NULL) > 0 ) { trio_asprintf(&q2, "delete from acls " "where aclid='%|Ss' and refcnt=0", aclid); - edg_wll_ExecStmt(ctx, q2, NULL); + edg_wll_ExecSQL(ctx, q2, NULL); } else { @@ -683,7 +683,7 @@ edg_wll_UpdateACL(edg_wll_Context ctx, edg_wlc_JobId job, trio_asprintf(&stmt, "update jobs set aclid='%|Ss' where jobid='%|Ss' and ISNULL(aclid)", new_aclid, md5_jobid); - updated = edg_wll_ExecStmt(ctx, stmt, NULL); + updated = edg_wll_ExecSQL(ctx, stmt, NULL); free(stmt); stmt = NULL; if (updated > 0) @@ -714,7 +714,7 @@ int edg_wll_GetACL(edg_wll_Context ctx, edg_wlc_JobId jobid, edg_wll_Acl *acl) char *q = NULL; char *acl_id = NULL; char *acl_str = NULL; - edg_wll_Stmt stmt = NULL; + glite_lbu_Statement stmt = NULL; int ret; GRSTgaclAcl *gacl = NULL; char *jobstr = edg_wlc_JobIdGetUnique(jobid); @@ -727,11 +727,11 @@ int edg_wll_GetACL(edg_wll_Context ctx, edg_wlc_JobId jobid, edg_wll_Acl *acl) trio_asprintf(&q, "select aclid from jobs where jobid = '%|Ss'", jobstr); - if (edg_wll_ExecStmt(ctx, q, &stmt) < 0 || - edg_wll_FetchRow(stmt, &acl_id) < 0) { + if (edg_wll_ExecSQL(ctx, q, &stmt) < 0 || + edg_wll_FetchRow(ctx, stmt, 1, NULL, &acl_id) < 0) { goto end; } - edg_wll_FreeStmt(&stmt); stmt = NULL; + glite_lbu_FreeStmt(&stmt); stmt = NULL; free(q); q = NULL; if (acl_id == NULL || *acl_id == '\0') { @@ -743,8 +743,8 @@ int edg_wll_GetACL(edg_wll_Context ctx, edg_wlc_JobId jobid, edg_wll_Acl *acl) trio_asprintf(&q, "select value from acls where aclid = '%|Ss'", acl_id); - if (edg_wll_ExecStmt(ctx, q, &stmt) < 0 || - edg_wll_FetchRow(stmt, &acl_str) < 0) { + if (edg_wll_ExecSQL(ctx, q, &stmt) < 0 || + edg_wll_FetchRow(ctx, stmt, 1, NULL, &acl_str) < 0) { goto end; } @@ -768,7 +768,7 @@ int edg_wll_GetACL(edg_wll_Context ctx, edg_wlc_JobId jobid, edg_wll_Acl *acl) end: if (q) free(q); - if (stmt) edg_wll_FreeStmt(&stmt); + if (stmt) glite_lbu_FreeStmt(&stmt); if (acl_id) free(acl_id); if (acl_str) free(acl_str); /* XXX if (gacl) GRSTgaclAclFree(gacl); */ diff --git a/org.glite.lb.server/src/lbs_db.c b/org.glite.lb.server/src/lbs_db.c deleted file mode 100644 index 4451fe5..0000000 --- a/org.glite.lb.server/src/lbs_db.c +++ /dev/null @@ -1,450 +0,0 @@ -#ident "$Header$" - -#include "mysql.h" // MySql header file -#include "mysqld_error.h" -#include "errmsg.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "glite/lbu/trio.h" -#include "lbs_db.h" -#include "glite/lb/context-int.h" - -#define my_err() edg_wll_SetError(ctx,EDG_WLL_ERROR_DB_CALL,mysql_error((MYSQL *) ctx->mysql)) - -struct _edg_wll_Stmt { - MYSQL_RES *result; - edg_wll_Context ctx; -}; - - -static edg_wll_ErrorCode db_connect(edg_wll_Context ctx, const char *cs, MYSQL **mysql) -{ - char *buf = NULL; - char *host,*user,*pw,*db; - char *slash,*at,*colon; - - if (!cs) cs = DEFAULTCS; - - if (!(*mysql = mysql_init(NULL))) - return edg_wll_SetError(ctx,ENOMEM,NULL); - - mysql_options(*mysql, MYSQL_READ_DEFAULT_FILE, "my"); - - host = user = pw = db = NULL; - - buf = strdup(cs); - slash = strchr(buf,'/'); - at = strrchr(buf,'@'); - colon = strrchr(buf,':'); - - if (!slash || !at || !colon) { - free(buf); - return edg_wll_SetError(ctx,EINVAL,"DB connect string"); - } - - *slash = *at = *colon = 0; - host = at+1; - user = buf; - pw = slash+1; - db = colon+1; - - /* ljocha: CLIENT_FOUND_ROWS added to make authorization check - * working in update_notif(). - * Hope it does not break anything else */ - if (!mysql_real_connect(*mysql,host,user,pw,db,0,NULL,CLIENT_FOUND_ROWS)) { - free(buf); - return my_err(); - } - - free(buf); - return edg_wll_ResetError(ctx); -} - - -static void db_close(MYSQL *mysql) { - mysql_close(mysql); -} - - -static int transaction_test(edg_wll_Context ctx, MYSQL *m2) { - MYSQL *m1; - char *desc, *cmd_create, *cmd_insert, *cmd_select, *cmd_drop; - int retval; - edg_wll_ErrorCode err; - pid_t pid; - - ctx->use_transactions = 1; - pid = getpid(); - - asprintf(&cmd_create, "create table test%d (item int) engine='innodb'", pid); - asprintf(&cmd_insert, "insert into test%d (item) values (1)", pid); - asprintf(&cmd_select, "select item from test%d", pid); - asprintf(&cmd_drop, "drop table test%d", pid); - - m1 = (MYSQL *)ctx->mysql; - edg_wll_ExecStmt(ctx, cmd_drop, NULL); - if (edg_wll_ExecStmt(ctx, cmd_create, NULL) != 0) { - edg_wll_ResetError(ctx); - goto err1; - } - if (edg_wll_Transaction(ctx) != 0) goto err2; - if (edg_wll_ExecStmt(ctx, cmd_insert, NULL) != 1) goto err2; - - ctx->mysql = (void *)m2; - if ((retval = edg_wll_ExecStmt(ctx, cmd_select, NULL)) == -1) goto err2; - ctx->use_transactions = (retval == 0); - - ctx->mysql = (void *)m1; - if (edg_wll_Commit(ctx) != 0) goto err2; - if (edg_wll_ExecStmt(ctx, cmd_drop, NULL) != 0) goto err1; - -#ifdef LBS_DB_PROFILE - fprintf(stderr, "[%d] use_transactions = %d\n", getpid(), ctx->use_transactions); -#endif - - goto ok; -err2: - err = edg_wll_Error(ctx, NULL, &desc); - edg_wll_ExecStmt(ctx, cmd_drop, NULL); - edg_wll_SetError(ctx, err, desc); -err1: - ctx->use_transactions = 0; -ok: - free(cmd_create); - free(cmd_insert); - free(cmd_select); - free(cmd_drop); - return edg_wll_Error(ctx, NULL, NULL); -} - - -edg_wll_ErrorCode edg_wll_DBConnect(edg_wll_Context ctx, const char *cs) -{ - return db_connect(ctx, cs, (MYSQL **)&ctx->mysql); -} - - -void edg_wll_DBClose(edg_wll_Context ctx) -{ - db_close((MYSQL *) ctx->mysql); - ctx->mysql = NULL; -} - - -int edg_wll_ExecStmt(edg_wll_Context ctx,char *txt,edg_wll_Stmt *stmt) -{ - int err; - int retry_nr = 0; - int do_reconnect = 0; -#ifdef LBS_DB_PROFILE - struct timeval start,end; - int pid; - - static struct timeval sum = { - tv_sec: 0, - tv_usec: 0 - }; -#endif - - edg_wll_ResetError(ctx); - - if (stmt) { - *stmt = NULL; - } -/* -fputs(txt,stderr); -putc(10,stderr); -*/ - -#ifdef LBS_DB_PROFILE - gettimeofday(&start,NULL); -#endif - - while (retry_nr == 0 || do_reconnect) { - do_reconnect = 0; - if (mysql_query((MYSQL *) ctx->mysql,txt)) { - /* error occured */ - switch (err = mysql_errno((MYSQL *) ctx->mysql)) { - case 0: - break; - case ER_DUP_ENTRY: - edg_wll_SetError(ctx,EEXIST,mysql_error((MYSQL *) ctx->mysql)); - return -1; - break; - case CR_SERVER_LOST: - if (retry_nr <= 0) - do_reconnect = 1; - break; - default: - my_err(); - return -1; - break; - } - } - retry_nr++; - } - - if (stmt) { - *stmt = malloc(sizeof(**stmt)); - if (!*stmt) { - edg_wll_SetError(ctx,ENOMEM,NULL); - return -1; - } - memset(*stmt,0,sizeof(**stmt)); - (**stmt).ctx = ctx; - (**stmt).result = mysql_store_result((MYSQL *) ctx->mysql); - if (!(**stmt).result) { - if (mysql_errno((MYSQL *) ctx->mysql)) { - my_err(); - return -1; - } - } - } else { - MYSQL_RES *r = mysql_store_result((MYSQL *) ctx->mysql); - mysql_free_result(r); - } -#ifdef LBS_DB_PROFILE - pid = getpid(); - gettimeofday(&end,NULL); - end.tv_usec -= start.tv_usec; - end.tv_sec -= start.tv_sec; - if (end.tv_usec < 0) { end.tv_sec--; end.tv_usec += 1000000; } - - sum.tv_usec += end.tv_usec; - sum.tv_sec += end.tv_sec + sum.tv_usec / 1000000; - sum.tv_usec -= 1000000 * (sum.tv_usec / 1000000); - fprintf(stderr,"[%d] %s\n[%d] %3ld.%06ld (sum: %3ld.%06ld)\n",pid,txt,pid,end.tv_sec,end.tv_usec,sum.tv_sec,sum.tv_usec); -#endif - - return mysql_affected_rows((MYSQL *) ctx->mysql); -} - -int edg_wll_FetchRow(edg_wll_Stmt stmt,char **res) -{ - MYSQL_ROW row; - edg_wll_Context ctx = stmt->ctx; - int nr,i; - unsigned long *len; - - edg_wll_ResetError(ctx); - - if (!stmt->result) return 0; - - if (!(row = mysql_fetch_row(stmt->result))) { - if (mysql_errno((MYSQL *) ctx->mysql)) { - my_err(); - return -1; - } else return 0; - } - - nr = mysql_num_fields(stmt->result); - len = mysql_fetch_lengths(stmt->result); - for (i=0; iresult))) cols[i++] = f->name; - return i == 0; -} - -void edg_wll_FreeStmt(edg_wll_Stmt *stmt) -{ - if (*stmt) { - if ((**stmt).result) mysql_free_result((**stmt).result); - free(*stmt); - *stmt = NULL; - } -} - -int edg_wll_DBCheckVersion(edg_wll_Context ctx, const char *cs) -{ - MYSQL *m = (MYSQL *) ctx->mysql; - MYSQL *m2; - const char *ver_s = mysql_get_server_info(m); - int major,minor,sub,version; - - if (!ver_s || 3 != sscanf(ver_s,"%d.%d.%d",&major,&minor,&sub)) - return edg_wll_SetError(ctx,EINVAL,"retreiving MySQL version"); - - version = 10000*major + 100*minor + sub; - - if (version < EDG_WLL_MYSQL_VERSION) { - char msg[300]; - - snprintf(msg,sizeof msg,"Your MySQL version is %d. At least %d required.",version,EDG_WLL_MYSQL_VERSION); - return edg_wll_SetError(ctx,EINVAL,msg); - } - - edg_wll_ResetError(ctx); - - if (db_connect(ctx, cs, &m2) == 0) { - transaction_test(ctx, m2); - db_close(m2); - } - - return edg_wll_Error(ctx, NULL, NULL); -} - - -int edg_wll_Transaction(edg_wll_Context ctx) { - if (ctx->use_transactions) { - if (edg_wll_ExecStmt(ctx, "set autocommit=0", NULL) < 0) goto err; - if (edg_wll_ExecStmt(ctx, "begin", NULL) < 0) goto err; - } -err: - return edg_wll_Error(ctx, NULL, NULL); -} - - -int edg_wll_Commit(edg_wll_Context ctx) { - if (ctx->use_transactions) { - if (edg_wll_ExecStmt(ctx, "commit", NULL) < 0) goto err; - if (edg_wll_ExecStmt(ctx, "set autocommit=1", NULL) < 0) goto err; - } -err: - return edg_wll_Error(ctx, NULL, NULL); -} - - -int edg_wll_Rollback(edg_wll_Context ctx) { - if (ctx->use_transactions) { - if (edg_wll_ExecStmt(ctx, "rollback", NULL) < 0) goto err; - if (edg_wll_ExecStmt(ctx, "set autocommit=1", NULL) < 0) goto err; - } -err: - return edg_wll_Error(ctx, NULL, NULL); -} - - -edg_wll_ErrorCode edg_wll_bufferedInsertInit(edg_wll_Context ctx, edg_wll_bufInsert *bi, void *mysql, char *table_name, long size_limit, long record_limit, char *columns) -{ - bi->ctx = ctx; - bi->table_name = strdup(table_name); - bi->columns = strdup(columns); - bi->rec_num = 0; - bi->rec_size = 0; - bi->rows = calloc(record_limit, sizeof(*(bi->rows)) );; - bi->size_limit = size_limit; - bi->record_limit = record_limit; - - return edg_wll_Error(bi->ctx,NULL,NULL); -; -} - - -#if 0 -static int string_add(char *what, long *used_size, long *alloc_size, char **where) -{ - long what_len = strlen(what); - int reall = 0; - - while (*used_size + what_len >= *alloc_size) { - *alloc_size += BUF_INSERT_ROW_ALLOC_BLOCK; - reall = 1; - } - - if (reall) - *where = realloc(*where, *alloc_size * sizeof(char)); - - what_len = sprintf(*where + *used_size, "%s", what); - if (what_len < 0) /* ENOMEM? */ return -1; - - *used_size += what_len; - - return 0; -} -#endif - - -static int flush_bufferd_insert(edg_wll_bufInsert *bi) -{ - char *stmt, *vals, *temp; - long i; - - - if (!bi->rec_num) - return edg_wll_Error(bi->ctx,NULL,NULL); - - asprintf(&vals,"(%s)", bi->rows[0]); - for (i=1; i < bi->rec_num; i++) { - // XXX: use string add (preallocated memory) - asprintf(&temp,"%s,(%s)", vals, bi->rows[i]); - free(vals); vals = temp; temp = NULL; - free(bi->rows[i]); - bi->rows[i] = NULL; - } - - trio_asprintf(&stmt, "insert into %|Ss(%|Ss) values %s;", - bi->table_name, bi->columns, vals); - - if (edg_wll_ExecStmt(bi->ctx,stmt,NULL) < 0) { - if (edg_wll_Error(bi->ctx,NULL,NULL) == EEXIST) - edg_wll_ResetError(bi->ctx); - } - - /* reset bi counters */ - bi->rec_size = 0; - bi->rec_num = 0; - - free(vals); - free(stmt); - - return edg_wll_Error(bi->ctx,NULL,NULL); -} - - -/* - * adds row of n values into n columns into an insert buffer - * if num. of rows or size of data oversteps the limits, real - * multi-row insert is done - */ -edg_wll_ErrorCode edg_wll_bufferedInsert(edg_wll_bufInsert *bi, char *row) -{ - bi->rows[bi->rec_num++] = strdup(row); - bi->rec_size += strlen(row); - - if ((bi->size_limit && bi->rec_size >= bi->size_limit) || - (bi->record_limit && bi->rec_num >= bi->record_limit)) - { - if (flush_bufferd_insert(bi)) - return edg_wll_Error(bi->ctx,NULL,NULL); - } - - return edg_wll_ResetError(bi->ctx); -} - -static void free_buffered_insert(edg_wll_bufInsert *bi) { - long i; - - free(bi->table_name); - free(bi->columns); - for (i=0; i < bi->rec_num; i++) { - free(bi->rows[i]); - } - free(bi->rows); -} - -edg_wll_ErrorCode edg_wll_bufferedInsertClose(edg_wll_bufInsert *bi) -{ - if (flush_bufferd_insert(bi)) - return edg_wll_Error(bi->ctx,NULL,NULL); - free_buffered_insert(bi); - - return edg_wll_ResetError(bi->ctx); -} - diff --git a/org.glite.lb.server/src/lbs_db_supp.c b/org.glite.lb.server/src/lbs_db_supp.c deleted file mode 100644 index ca00a6d..0000000 --- a/org.glite.lb.server/src/lbs_db_supp.c +++ /dev/null @@ -1,32 +0,0 @@ -#ident "$Header$" - -#include -#include -#include -#include - -char *edg_wll_TimeToDB(time_t t) -{ - struct tm *tm = gmtime(&t); - char tbuf[256]; - - sprintf(tbuf,"'%4d-%02d-%02d %02d:%02d:%02d'",tm->tm_year+1900,tm->tm_mon+1, - tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec); - - return strdup(tbuf); -} - -time_t edg_wll_DBToTime(char *t) -{ - struct tm tm; - - memset(&tm,0,sizeof(tm)); - setenv("TZ","UTC",1); tzset(); - sscanf(t,"%4d-%02d-%02d %02d:%02d:%02d", - &tm.tm_year,&tm.tm_mon,&tm.tm_mday, - &tm.tm_hour,&tm.tm_min,&tm.tm_sec); - tm.tm_year -= 1900; - tm.tm_mon--; - - return mktime(&tm); -} diff --git a/org.glite.lb.server/src/load.c b/org.glite.lb.server/src/load.c index 925c53a..6cacb0d 100644 --- a/org.glite.lb.server/src/load.c +++ b/org.glite.lb.server/src/load.c @@ -21,10 +21,10 @@ #include "store.h" #include "purge.h" -#include "lbs_db.h" #include "query.h" #include "get_events.h" #include "server_state.h" +#include "db_supp.h" static int read_line(char **buff, size_t *maxsize, int fd); diff --git a/org.glite.lb.server/src/mon-db.c b/org.glite.lb.server/src/mon-db.c index a077771..50d9878 100644 --- a/org.glite.lb.server/src/mon-db.c +++ b/org.glite.lb.server/src/mon-db.c @@ -11,8 +11,7 @@ #include "glite/jobid/strmd5.h" #include "glite/lb/context-int.h" #include "glite/lb/jobstat.h" - -#include "lbs_db.h" +#include "db_supp.h" static struct option opts[] = { { "mysql",1,NULL,'m' }, @@ -29,13 +28,13 @@ static const char *me; int main(int argc,char **argv) { - int opt; + int opt, caps; char *dbstring = getenv("LBDB"); int verbose = 0, rows = 0, fields = 0, njobs = 0, i; edg_wll_Context ctx; char *stmt = NULL, *status = NULL; char *str[2]; - edg_wll_Stmt sh; + glite_lbu_Statement sh; int jobs[EDG_WLL_NUMBER_OF_STATCODES]; me = strdup(argv[0]); @@ -50,16 +49,16 @@ int main(int argc,char **argv) edg_wll_InitContext(&ctx); for (i = 1; idbctx)) < 0 || !(caps & GLITE_LBU_DB_CAP_INDEX)) do_exit(ctx,EX_SOFTWARE); if (asprintf(&stmt,"SELECT status,count(status) FROM states GROUP BY status;") < 0) do_exit(ctx,EX_OSERR); if (verbose) fprintf(stderr,"mysql query: %s\n",stmt); - if ((rows = edg_wll_ExecStmt(ctx,stmt,&sh)) < 0) do_exit(ctx,EX_SOFTWARE); + if ((rows = edg_wll_ExecSQL(ctx,stmt,&sh)) < 0) do_exit(ctx,EX_SOFTWARE); if (verbose) fprintf(stderr,"number of states returned: %d\n",rows); if (rows > 0) fprintf(stdout,"Number of jobs in each state: \n"); for (i = 0; i < rows; i++) { - fields = edg_wll_FetchRow(sh, str); + fields = edg_wll_FetchRow(ctx, sh, sizeof(str)/sizeof(str[0]),NULL,str); if (fields != 2) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); do_exit(ctx,EX_SOFTWARE); } jobs[atoi(str[0])] = atoi(str[1]); @@ -75,7 +74,7 @@ int main(int argc,char **argv) fprintf(stdout,"Total number of jobs: %d\n",njobs); if (stmt) free(stmt); - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); edg_wll_FreeContext(ctx); return 0; diff --git a/org.glite.lb.server/src/notif_match.c b/org.glite.lb.server/src/notif_match.c index 045edf5..6954152 100644 --- a/org.glite.lb.server/src/notif_match.c +++ b/org.glite.lb.server/src/notif_match.c @@ -10,11 +10,11 @@ #include "glite/lb/context-int.h" #include "glite/lbu/trio.h" -#include "lbs_db.h" #include "lb_authz.h" #include "lb_xml_parse.h" #include "query.h" #include "il_notification.h" +#include "db_supp.h" static int notif_match_conditions(edg_wll_Context,const edg_wll_JobStat *,const char *); static int notif_check_acl(edg_wll_Context,const edg_wll_JobStat *,const char *); @@ -25,7 +25,7 @@ int edg_wll_NotifMatch(edg_wll_Context ctx, const edg_wll_JobStat *stat) { edg_wll_NotifId nid = NULL; char *jobq,*ju = NULL,*jobc[5]; - edg_wll_Stmt jobs = NULL; + glite_lbu_Statement jobs = NULL; int ret,i; time_t now = time(NULL); @@ -45,10 +45,10 @@ int edg_wll_NotifMatch(edg_wll_Context ctx, const edg_wll_JobStat *stat) free(ju); - if (edg_wll_ExecStmt(ctx,jobq,&jobs) < 0) goto err; + if (edg_wll_ExecSQL(ctx,jobq,&jobs) < 0) goto err; - while ((ret = edg_wll_FetchRow(jobs,jobc)) > 0) { - if (now > edg_wll_DBToTime(jobc[2])) + while ((ret = edg_wll_FetchRow(ctx,jobs,sizeof(jobc)/sizeof(jobc[0]),NULL,jobc)) > 0) { + if (now > glite_lbu_DBToTime(jobc[2])) edg_wll_NotifExpired(ctx,jobc[0]); else if (notif_match_conditions(ctx,stat,jobc[4]) && notif_check_acl(ctx,stat,jobc[3])) @@ -96,7 +96,7 @@ int edg_wll_NotifMatch(edg_wll_Context ctx, const edg_wll_JobStat *stat) err: if ( nid ) edg_wll_NotifIdFree(nid); free(jobq); - edg_wll_FreeStmt(&jobs); + glite_lbu_FreeStmt(&jobs); return edg_wll_Error(ctx,NULL,NULL); } diff --git a/org.glite.lb.server/src/notification.c b/org.glite.lb.server/src/notification.c index f068f2a..57af532 100644 --- a/org.glite.lb.server/src/notification.c +++ b/org.glite.lb.server/src/notification.c @@ -12,8 +12,8 @@ #include "glite/lb/xml_parse.h" #include "il_notification.h" -#include "lbs_db.h" #include "query.h" +#include "db_supp.h" static char *get_user(edg_wll_Context ctx, int create); @@ -79,8 +79,9 @@ int edg_wll_NotifNewServer( *valid = ctx->peerProxyValidity; else *valid += ctx->notifDuration; - - if ( !(time_s = strdup(edg_wll_TimeToDB(*valid))) ) + + glite_lbu_TimeToDB(*valid, &time_s); + if ( !time_s ) { edg_wll_SetError(ctx, errno, NULL); goto cleanup; @@ -108,7 +109,7 @@ int edg_wll_NotifNewServer( "values ('%|Ss','%|Ss',%s,'%|Ss', '%|Ss')", nid_s, addr_s? addr_s: address_override, time_s, owner, xml_conds); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) goto cleanup; if (jobs) for ( i = 0; jobs[i]; i++ ) @@ -117,24 +118,24 @@ int edg_wll_NotifNewServer( trio_asprintf(&q, "insert into notif_jobs(notifid,jobid) values ('%|Ss','%|Ss')", nid_s, jobs[i]); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) { /* XXX: Remove uncoplete registration? * Which error has to be returned? */ free(q); trio_asprintf(&q, "delete from notif_jobs where notifid='%|Ss'", nid_s); - edg_wll_ExecStmt(ctx, q, NULL); + edg_wll_ExecSQL(ctx, q, NULL); free(q); trio_asprintf(&q, "delete from notif_registrations where notifid='%|Ss'", nid_s); - edg_wll_ExecStmt(ctx, q, NULL); + edg_wll_ExecSQL(ctx, q, NULL); goto cleanup; } } else { trio_asprintf(&q,"insert into notif_jobs(notifid,jobid) values ('%|Ss','%|Ss')", nid_s,NOTIF_ALL_JOBS); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) goto cleanup; + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) goto cleanup; } @@ -186,7 +187,8 @@ int edg_wll_NotifBindServer( else *valid += ctx->notifDuration; - if ( !(time_s = strdup(edg_wll_TimeToDB(*valid))) ) + glite_lbu_TimeToDB(*valid, &time_s); + if ( !time_s ) { edg_wll_SetError(ctx, errno, "Formating validity time"); goto cleanup; @@ -272,7 +274,7 @@ int edg_wll_NotifChangeServer( /* Format DB insert statement */ trio_asprintf(&q, "delete from notif_jobs where notifid='%|Ss'", nid_s); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) goto cleanup; for ( i = 0; jobs[i]; i++ ) @@ -281,17 +283,17 @@ int edg_wll_NotifChangeServer( trio_asprintf(&q, "insert into notif_jobs(notifid,jobid) values ('%|Ss','%|Ss')", nid_s, jobs[i]); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) { /* XXX: Remove uncoplete registration? * Which error has to be returned? */ free(q); trio_asprintf(&q, "delete from notif_jobs where notifid='%|Ss'", nid_s); - edg_wll_ExecStmt(ctx, q, NULL); + edg_wll_ExecSQL(ctx, q, NULL); free(q); trio_asprintf(&q,"delete from notif_registrations where notifid='%|Ss'", nid_s); - edg_wll_ExecStmt(ctx, q, NULL); + edg_wll_ExecSQL(ctx, q, NULL); goto cleanup; } } @@ -341,7 +343,8 @@ int edg_wll_NotifRefreshServer( else *valid += ctx->notifDuration; - if ( !(time_s = strdup(edg_wll_TimeToDB(*valid))) ) + glite_lbu_TimeToDB(*valid, &time_s); + if ( !time_s ) { edg_wll_SetError(ctx, errno, "Formating validity time"); goto cleanup; @@ -371,11 +374,11 @@ int edg_wll_NotifDropServer( goto cleanup; trio_asprintf(&stmt, "delete from notif_registrations where notifid='%|Ss'", nid_s); - if ( (ret = edg_wll_ExecStmt(ctx, stmt, NULL)) < 0 ) + if ( (ret = edg_wll_ExecSQL(ctx, stmt, NULL)) < 0 ) goto cleanup; free(stmt); trio_asprintf(&stmt, "delete from notif_jobs where notifid='%|Ss'", nid_s); - edg_wll_ExecStmt(ctx, stmt, NULL); + edg_wll_ExecSQL(ctx, stmt, NULL); edg_wll_NotifCancelRegId(ctx, nid); cleanup: @@ -388,7 +391,7 @@ cleanup: static char *get_user(edg_wll_Context ctx, int create) { - edg_wll_Stmt stmt = NULL; + glite_lbu_Statement stmt = NULL; char *userid = NULL, *q = NULL; int ret; @@ -400,7 +403,7 @@ static char *get_user(edg_wll_Context ctx, int create) goto cleanup; } trio_asprintf(&q, "select userid from users where cert_subj='%|Ss'", ctx->peerName); - if ( edg_wll_ExecStmt(ctx, q, &stmt) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, &stmt) < 0 ) goto cleanup; /* returned value: @@ -408,7 +411,7 @@ static char *get_user(edg_wll_Context ctx, int create) * >0 user found - return selected value * <0 SQL error */ - if ( ((ret = edg_wll_FetchRow(stmt, &userid)) != 0) || !create ) + if ( ((ret = edg_wll_FetchRow(ctx, stmt, 1, NULL, &userid)) != 0) || !create ) goto cleanup; if ( !(userid = strdup(strmd5(ctx->peerName, NULL))) ) @@ -419,7 +422,7 @@ static char *get_user(edg_wll_Context ctx, int create) free(q); trio_asprintf(&q, "insert into users(userid,cert_subj) values ('%|Ss','%|Ss')", userid, ctx->peerName); - if ( edg_wll_ExecStmt(ctx, q, NULL) < 0 ) + if ( edg_wll_ExecSQL(ctx, q, NULL) < 0 ) { if ( edg_wll_Error(ctx,NULL,NULL) != EEXIST ) { @@ -432,7 +435,7 @@ static char *get_user(edg_wll_Context ctx, int create) cleanup: if ( q ) free(q); - if ( stmt ) edg_wll_FreeStmt(&stmt); + if ( stmt ) glite_lbu_FreeStmt(&stmt); return userid; } @@ -464,14 +467,14 @@ static int check_notif_request( "where notifid='%|Ss' and userid='%|Ss'", nid_s, user); - if ( (ret = edg_wll_ExecStmt(ctx, stmt, NULL)) < 0 ) + if ( (ret = edg_wll_ExecSQL(ctx, stmt, NULL)) < 0 ) goto cleanup; if ( ret == 0 ) { free(stmt); trio_asprintf(&stmt, "select notifid from notif_registrations where notifid='%|Ss'", nid_s); - ret = edg_wll_ExecStmt(ctx, stmt, NULL); + ret = edg_wll_ExecSQL(ctx, stmt, NULL); if ( ret == 0 ) edg_wll_SetError(ctx, ENOENT, "Unknown notification ID"); else if ( ret > 0 ) @@ -617,14 +620,14 @@ static int update_notif( free(stmt); stmt = aux; - if ( (ret = edg_wll_ExecStmt(ctx, stmt, NULL)) < 0 ) + if ( (ret = edg_wll_ExecSQL(ctx, stmt, NULL)) < 0 ) goto cleanup; if ( ret == 0 ) { free(stmt); trio_asprintf(&stmt, "select notifid from notif_registrations where notifid='%|Ss'", nid_s); - ret = edg_wll_ExecStmt(ctx, stmt, NULL); + ret = edg_wll_ExecSQL(ctx, stmt, NULL); if ( ret == 0 ) edg_wll_SetError(ctx, ENOENT, "Unknown notification ID"); /* diff --git a/org.glite.lb.server/src/openserver.c b/org.glite.lb.server/src/openserver.c index 215418f..77b8d93 100644 --- a/org.glite.lb.server/src/openserver.c +++ b/org.glite.lb.server/src/openserver.c @@ -1,15 +1,18 @@ #ident "$Header$" +#include "glite/lbu/db.h" #include "glite/lb/context-int.h" -#include "lbs_db.h" - edg_wll_ErrorCode edg_wll_Open(edg_wll_Context ctx, char *cs) { - return edg_wll_DBConnect(ctx,cs) ? edg_wll_Error(ctx,NULL,NULL) : 0; + glite_lbu_InitDBContext(&ctx->dbctx); + return glite_lbu_DBConnect(ctx->dbctx,cs) ? edg_wll_SetErrorDB(ctx) : 0; } edg_wll_ErrorCode edg_wll_Close(edg_wll_Context ctx) { + glite_lbu_DBClose(ctx->dbctx); + glite_lbu_FreeDBContext(ctx->dbctx); + ctx->dbctx = NULL; return edg_wll_ResetError(ctx); } diff --git a/org.glite.lb.server/src/query.c b/org.glite.lb.server/src/query.c index 52cd621..5133ea9 100644 --- a/org.glite.lb.server/src/query.c +++ b/org.glite.lb.server/src/query.c @@ -21,6 +21,7 @@ #include "query.h" #include "store.h" #include "lb_authz.h" +#include "db_supp.h" #define FL_SEL_STATUS 1 #define FL_SEL_TAGS (1<<1) @@ -55,7 +56,7 @@ int edg_wll_QueryEventsServer( *q = NULL, *res[11]; edg_wll_Event *out = NULL; - edg_wll_Stmt sh = NULL; + glite_lbu_Statement sh = NULL; int i = 0, ret = 0, offset = 0, limit = 0, @@ -132,12 +133,12 @@ int edg_wll_QueryEventsServer( q = qbase; // printf("\nquery: %s\n\n", q); - ret = edg_wll_ExecStmt(ctx, q, &sh); + ret = edg_wll_ExecSQL(ctx, q, &sh); if ( limit ) free(q); if ( ret < 0 ) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); goto cleanup; } if ( ret == 0 ) @@ -149,7 +150,7 @@ int edg_wll_QueryEventsServer( limit_loop = 0; offset += ret; - while ( (ret = edg_wll_FetchRow(sh, res)) == sizofa(res) ) + while ( (ret = edg_wll_FetchRow(ctx, sh, sizofa(res), NULL, res)) == sizofa(res) ) { int n = atoi(res[0]); free(res[0]); @@ -160,7 +161,7 @@ int edg_wll_QueryEventsServer( free(res[1]); free(res[2]); memset(out+i, 0, sizeof(*out)); - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); goto cleanup; } @@ -254,7 +255,7 @@ fetch_cycle_cleanup: j_old=res[2]; } limit_cycle_cleanup: - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); } while ( limit_loop ); if ( i == 0 && eperm ) @@ -299,7 +300,7 @@ int edg_wll_QueryJobsServer( *res[3]; edg_wlc_JobId *jobs_out = NULL; edg_wll_JobStat *states_out = NULL; - edg_wll_Stmt sh; + glite_lbu_Statement sh; int i = 0, ret = 0, eperm = 0, @@ -367,12 +368,12 @@ int edg_wll_QueryJobsServer( q = qbase; // printf("\nquery: %s\n\n", q); - ret = edg_wll_ExecStmt(ctx, q, &sh); + ret = edg_wll_ExecSQL(ctx, q, &sh); if ( limit ) free(q); if ( ret < 0 ) { - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); goto cleanup; } if ( ret == 0 ) @@ -384,7 +385,7 @@ int edg_wll_QueryJobsServer( limit_loop = 0; offset += ret; - while ( (ret=edg_wll_FetchRow(sh,res)) > 0 ) + while ( (ret=edg_wll_FetchRow(ctx,sh,sizofa(res),NULL,res)) > 0 ) { if ( (ret = edg_wlc_JobIdParse(res[0], jobs_out+i)) ) { /* unlikely to happen, internal inconsistency */ @@ -456,7 +457,7 @@ fetch_cycle_cleanup: jobs_out[i] = NULL; } limit_cycle_cleanup: - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); } while ( limit_loop ); if ( !*jobs_out ) { @@ -664,14 +665,16 @@ static char *ec_to_head_where(edg_wll_Context ctx,const edg_wll_QueryRec **ec) for ( n = 0; ec[m][n].attr; n++ ) switch ( ec[m][n].attr ) { case EDG_WLL_QUERY_ATTR_TIME: - dbt = edg_wll_TimeToDB(ec[m][n].value.t.tv_sec); + glite_lbu_TimeToDB(ec[m][n].value.t.tv_sec, &dbt); if ( conds ) { if ( ec[m][n].op == EDG_WLL_QUERY_OP_WITHIN ) { trio_asprintf(&aux, "%s", dbt); - dbt = edg_wll_TimeToDB(ec[m][n].value2.t.tv_sec); + free(dbt); + glite_lbu_TimeToDB(ec[m][n].value2.t.tv_sec, &dbt); trio_asprintf(&out, "%s OR (e.time_stamp >= %s AND e.time_stamp <= %s)", conds, aux, dbt); + free(dbt); free(aux); } else if (ec[m][n].op == EDG_WLL_QUERY_OP_EQUAL) { @@ -686,8 +689,10 @@ static char *ec_to_head_where(edg_wll_Context ctx,const edg_wll_QueryRec **ec) else if ( ec[m][n].op == EDG_WLL_QUERY_OP_WITHIN ) { trio_asprintf(&aux, "%s", dbt); - dbt = edg_wll_TimeToDB(ec[m][n].value2.t.tv_sec); + free(dbt); + glite_lbu_TimeToDB(ec[m][n].value2.t.tv_sec, &dbt); trio_asprintf(&conds, "(e.time_stamp >= %s AND e.time_stamp <= %s)", aux, dbt); + free(dbt); free(aux); } else if (ec[m][n].op == EDG_WLL_QUERY_OP_EQUAL) { @@ -935,14 +940,16 @@ static char *jc_to_head_where( *where_flags |= FL_SEL_STATUS; - dbt = edg_wll_TimeToDB(jc[m][n].value.t.tv_sec); + glite_lbu_TimeToDB(jc[m][n].value.t.tv_sec, &dbt); if ( conds ) { if ( jc[m][n].op == EDG_WLL_QUERY_OP_WITHIN ) { trio_asprintf(&aux, "%s", dbt); - dbt = edg_wll_TimeToDB(jc[m][n].value2.t.tv_sec); + free(dbt); + glite_lbu_TimeToDB(jc[m][n].value2.t.tv_sec, &dbt); trio_asprintf(&tmps, "%s OR (s.%s >= %s AND s.%s <= %s)", conds, cname, aux, cname, dbt); + free(dbt); free(aux); } else @@ -954,8 +961,10 @@ static char *jc_to_head_where( else if ( jc[m][n].op == EDG_WLL_QUERY_OP_WITHIN ) { trio_asprintf(&aux, "%s", dbt); - dbt = edg_wll_TimeToDB(jc[m][n].value2.t.tv_sec); + free(dbt); + glite_lbu_TimeToDB(jc[m][n].value2.t.tv_sec, &dbt); trio_asprintf(&conds, "(s.%s >= %s AND s.%s <= %s)", cname, aux, cname, dbt); + free(dbt); free(aux); } else @@ -1219,7 +1228,7 @@ int convert_event_head(edg_wll_Context ctx,char **f,edg_wll_Event *e) e->any.user = f[4]; f[4] = NULL; - e->any.timestamp.tv_sec = edg_wll_DBToTime(f[5]); + e->any.timestamp.tv_sec = glite_lbu_DBToTime(f[5]); free(f[5]); f[5] = NULL; e->any.timestamp.tv_usec = atoi(f[6]); @@ -1228,7 +1237,7 @@ int convert_event_head(edg_wll_Context ctx,char **f,edg_wll_Event *e) e->any.level = atoi(f[7]); free(f[7]); f[7] = NULL; - e->any.arrived.tv_sec = edg_wll_DBToTime(f[8]); + e->any.arrived.tv_sec = glite_lbu_DBToTime(f[8]); e->any.arrived.tv_usec = 0; free(f[8]); f[8] = NULL; diff --git a/org.glite.lb.server/src/request.c b/org.glite.lb.server/src/request.c index 1e08608..4e4c39c 100644 --- a/org.glite.lb.server/src/request.c +++ b/org.glite.lb.server/src/request.c @@ -10,7 +10,7 @@ #include "glite/lb/context-int.h" #include "store.h" -#include "lbs_db.h" +#include "db_supp.h" #ifdef __GNUC__ #define UNUSED_VAR __attribute__((unused)) diff --git a/org.glite.lb.server/src/seqcode.c b/org.glite.lb.server/src/seqcode.c index 5765e01..2edbcc8 100644 --- a/org.glite.lb.server/src/seqcode.c +++ b/org.glite.lb.server/src/seqcode.c @@ -12,12 +12,13 @@ /* #include "lb_authz.h" */ +#include "db_supp.h" int edg_wll_QuerySequenceCodeServer(edg_wll_Context ctx, edg_wlc_JobId jobid, const char *source, char **seqcode) { - edg_wll_Stmt sh; + glite_lbu_Statement sh; intJobStat *istat = NULL; char *jobid_md5 = NULL, *stmt = NULL, @@ -36,12 +37,12 @@ int edg_wll_QuerySequenceCodeServer(edg_wll_Context ctx, edg_wlc_JobId jobid, co if ( stmt == NULL ) return edg_wll_SetError(ctx, ENOMEM, NULL); - if ( (nstates = edg_wll_ExecStmt(ctx, stmt, &sh)) < 0 ) goto cleanup; + if ( (nstates = edg_wll_ExecSQL(ctx, stmt, &sh)) < 0 ) goto cleanup; if ( nstates == 0 ) { edg_wll_SetError(ctx, ENOENT, "no state in DB"); goto cleanup; } - if ( edg_wll_FetchRow(sh, &res) < 0 ) goto cleanup; + if ( edg_wll_FetchRow(ctx, sh, 1, NULL, &res) < 0 ) goto cleanup; istat = dec_intJobStat(res, &res_rest); if ( res_rest && istat ) { @@ -55,7 +56,7 @@ cleanup: free(res); free(jobid_md5); free(stmt); - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); if ( istat ) { destroy_intJobStat(istat); free(istat); diff --git a/org.glite.lb.server/src/server_state.c b/org.glite.lb.server/src/server_state.c index 9c5778f..c2bd7bf 100644 --- a/org.glite.lb.server/src/server_state.c +++ b/org.glite.lb.server/src/server_state.c @@ -5,26 +5,26 @@ #include "glite/lbu/trio.h" #include "glite/lb/context-int.h" -#include "lbs_db.h" #include "server_state.h" +#include "db_supp.h" int edg_wll_GetServerState(edg_wll_Context ctx,const char *name,char **val) { char *stmt = NULL; - edg_wll_Stmt q = NULL; + glite_lbu_Statement q = NULL; trio_asprintf(&stmt,"select value from server_state " "where prefix = 'https://%|Ss:%d' and name = '%|Ss'", ctx->srvName,ctx->srvPort,name); - switch (edg_wll_ExecStmt(ctx,stmt,&q)) { + switch (edg_wll_ExecSQL(ctx,stmt,&q)) { case 0: edg_wll_SetError(ctx,ENOENT,name); break; case -1: break; - default: edg_wll_FetchRow(q,val); break; + default: edg_wll_FetchRow(ctx,q,sizeof(val)/sizeof(val[0]),NULL,val); break; } - edg_wll_FreeStmt(&q); + glite_lbu_FreeStmt(&q); free(stmt); return edg_wll_Error(ctx,NULL,NULL); } @@ -37,7 +37,7 @@ int edg_wll_SetServerState(edg_wll_Context ctx,const char *name,const char *val) "values ('https://%|Ss:%d','%|Ss','%|Ss')", ctx->srvName,ctx->srvPort,name,val); - switch(edg_wll_ExecStmt(ctx,stmt,NULL)) { + switch(edg_wll_ExecSQL(ctx,stmt,NULL)) { case 1: break; case -1: if (edg_wll_Error(ctx,NULL,NULL) == EEXIST) { free(stmt); @@ -45,7 +45,7 @@ int edg_wll_SetServerState(edg_wll_Context ctx,const char *name,const char *val) "where prefix = 'https://%|Ss:%d' " "and name = '%|Ss'", val,ctx->srvName,ctx->srvPort,name); - edg_wll_ExecStmt(ctx,stmt,NULL); + edg_wll_ExecSQL(ctx,stmt,NULL); } break; diff --git a/org.glite.lb.server/src/srv_purge.c b/org.glite.lb.server/src/srv_purge.c index 0b3e321..18b505f 100644 --- a/org.glite.lb.server/src/srv_purge.c +++ b/org.glite.lb.server/src/srv_purge.c @@ -22,12 +22,12 @@ #include "lb_proto.h" #include "store.h" #include "lock.h" -#include "lbs_db.h" #include "query.h" #include "get_events.h" #include "purge.h" #include "lb_xml_parse.h" #include "db_calls.h" +#include "db_supp.h" #define DUMP_FILE_STORAGE "/tmp/" @@ -45,11 +45,6 @@ static const char* const resp_headers[] = { static int purge_one(edg_wll_Context ctx,const edg_wlc_JobId,int,int); static int unset_proxy_flag(edg_wll_Context ctx, edg_wlc_JobId job); -/** - * return job membership in DB table jobs (proxy, server, both) - */ -static int edg_wll_jobMembership(edg_wll_Context ctx, edg_wlc_JobId job); - int edg_wll_CreateTmpFileStorage(edg_wll_Context ctx, char *prefix, char **fname) { @@ -205,6 +200,8 @@ int edg_wll_PurgeServerProxy(edg_wll_Context ctx, edg_wlc_JobId job) case DB_SERVER_JOB: // should not happen, however, no action needed // proxy flag is unset already + edg_wll_ResetError(ctx); + return 0; break; case DB_PROXY_JOB+DB_SERVER_JOB: return(unset_proxy_flag(ctx, job)); @@ -283,7 +280,7 @@ int edg_wll_PurgeServer(edg_wll_Context ctx,const edg_wll_PurgeRequest *request) } } else { - edg_wll_Stmt s; + glite_lbu_Statement s; char *job_s; int res; time_t timeout[EDG_WLL_NUMBER_OF_STATCODES], @@ -292,8 +289,8 @@ int edg_wll_PurgeServer(edg_wll_Context ctx,const edg_wll_PurgeRequest *request) for (i=0; itimeout[i] < 0 ? ctx->purge_timeout[i] : request->timeout[i]; - if (edg_wll_ExecStmt(ctx,"select dg_jobid from jobs",&s) < 0) goto abort; - while ((res = edg_wll_FetchRow(s,&job_s)) > 0) { + if (edg_wll_ExecSQL(ctx,"select dg_jobid from jobs",&s) < 0) goto abort; + while ((res = edg_wll_FetchRow(ctx,s,1,NULL,&job_s)) > 0) { if (edg_wlc_JobIdParse(job_s,&job)) { fprintf(stderr,"%s: parse error (internal inconsistency !)\n",job_s); parse = 1; @@ -341,7 +338,7 @@ int edg_wll_PurgeServer(edg_wll_Context ctx,const edg_wll_PurgeRequest *request) free(job_s); } } - edg_wll_FreeStmt(&s); + glite_lbu_FreeStmt(&s); abort: // just for escaping from nested cycles ; /* prevent compiler to complain */ @@ -436,7 +433,7 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) { char *dbjob; char *stmt = NULL; - edg_wll_Stmt q; + glite_lbu_Statement q; int ret,dumped = 0; edg_wll_ResetError(ctx); @@ -448,7 +445,7 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) if ( purge ) { trio_asprintf(&stmt,"delete from jobs where jobid = '%|Ss'",dbjob); - ret = edg_wll_ExecStmt(ctx,stmt,NULL); + ret = edg_wll_ExecSQL(ctx,stmt,NULL); if (ret <= 0) { unlock_and_check(ctx,job); if (ret == 0) { @@ -460,7 +457,7 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) free(stmt); stmt = NULL; trio_asprintf(&stmt,"delete from states where jobid = '%|Ss'",dbjob); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { unlock_and_check(ctx,job); goto clean; } @@ -468,7 +465,7 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) /* Why on earth ? trio_asprintf(&stmt,"delete from states where jobid = '%|Ss'",dbjob); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { unlock_and_check(ctx,job); goto clean; } @@ -482,7 +479,7 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) if ( purge ) { trio_asprintf(&stmt,"delete from status_tags where jobid = '%|Ss'",dbjob); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) goto unlock; + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) goto unlock; free(stmt); stmt = NULL; } @@ -499,11 +496,11 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) "order by event", dbjob); /* check for events repeatedly -- new one may have arrived in the meantime */ - while ((ret = edg_wll_ExecStmt(ctx,stmt,&q)) > 0) { + while ((ret = edg_wll_ExecSQL(ctx,stmt,&q)) > 0) { char *res[9]; dumped = 1; - while ((ret = edg_wll_FetchRow(q,res)) > 0) { + while ((ret = edg_wll_FetchRow(ctx,q,sizofa(res),NULL,res)) > 0) { int event; event = atoi(res[0]); @@ -584,11 +581,11 @@ int purge_one(edg_wll_Context ctx,const edg_wlc_JobId job,int dump, int purge) } } } - edg_wll_FreeStmt(&q); + glite_lbu_FreeStmt(&q); if (ret < 0 || !purge) break; } - edg_wll_FreeStmt(&q); + glite_lbu_FreeStmt(&q); unlock: if (ctx->strict_locking) unlock_and_check(ctx,job); @@ -611,6 +608,6 @@ int unset_proxy_flag(edg_wll_Context ctx, edg_wlc_JobId job) trio_asprintf(&stmt,"update jobs set proxy='0' where jobid='%|Ss'", dbjob); - return(edg_wll_ExecStmt(ctx,stmt,NULL)); + return(edg_wll_ExecSQL(ctx,stmt,NULL)); } diff --git a/org.glite.lb.server/src/store.c.T b/org.glite.lb.server/src/store.c.T index 1b8cd00..a30734e 100644 --- a/org.glite.lb.server/src/store.c.T +++ b/org.glite.lb.server/src/store.c.T @@ -26,16 +26,16 @@ #include "store.h" #include "get_events.h" -#include "lbs_db.h" #include "lock.h" #include "lb_authz.h" #include "jobstat.h" #include "db_calls.h" +#include "db_supp.h" static int store_user(edg_wll_Context,const char *,const char *); static int store_job(edg_wll_Context,const edg_wlc_JobId,const char *, int, int); #ifdef LB_BUF -static int store_job_block(edg_wll_Context, const edg_wlc_JobId, const char *, edg_wll_bufInsert *, int, int); +static int store_job_block(edg_wll_Context, const edg_wlc_JobId, const char *, glite_lbu_bufInsert *, int, int); #endif static int store_job_grey(edg_wll_Context,const edg_wlc_JobId,time_t); static int store_flesh(edg_wll_Context,edg_wll_Event *,char *,int); @@ -56,7 +56,7 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) edg_wll_ErrorCode err = 0; char *userid = NULL,*jobid,*stmt; char *select_max,*ssrc; - edg_wll_Stmt sh = NULL; + glite_lbu_Statement sh = NULL; int next = 0xDEAD; int lbproxy_notreg = 0; char *now_s = NULL; @@ -64,9 +64,9 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) ssrc = jobid = stmt = select_max = NULL; if ( ctx->event_load ) - now_s = strdup(edg_wll_TimeToDB(e->any.arrived.tv_sec)); + glite_lbu_TimeToDB(e->any.arrived.tv_sec, &now_s); else - now_s = strdup(edg_wll_TimeToDB(time(NULL))); + glite_lbu_TimeToDB(time(NULL), &now_s); edg_wll_ResetError(ctx); switch (err = check_auth(ctx,e)) { @@ -99,8 +99,8 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) trio_asprintf(&stmt,"select userid from jobs where jobid='%|Ss'", jobid); - if (edg_wll_ExecStmt(ctx,stmt,&sh) < 0 || edg_wll_FetchRow(sh,&userid) < 0) goto clean; - if (sh) edg_wll_FreeStmt(&sh); + if (edg_wll_ExecSQL(ctx,stmt,&sh) < 0 || edg_wll_FetchRow(ctx,sh,1,NULL,&userid) < 0) goto clean; + if (sh) glite_lbu_FreeStmt(&sh); /* obtain next event sequence number */ @@ -112,15 +112,15 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) /* try to insert (someone else may be doing the same) */ while (1) { - char *max; + char *max, *stamp; - if (edg_wll_ExecStmt(ctx,select_max,&sh) < 0 || - edg_wll_FetchRow(sh,&max) < 0) + if (edg_wll_ExecSQL(ctx,select_max,&sh) < 0 || + edg_wll_FetchRow(ctx,sh,1,NULL,&max) < 0) { err = edg_wll_Error(ctx,NULL,NULL); goto clean; } - edg_wll_FreeStmt(&sh); + glite_lbu_FreeStmt(&sh); next = max && *max ? atoi(max)+1 : 0; @@ -128,20 +128,21 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) * 1) when using transactions: * Store the whole event right now. * - * 2) when not using transaction: + * 2) when not using transactions: * Store an UNDEF event first in order to prevent race condition * with readers and update event code later. */ + glite_lbu_TimeToDB(e->any.timestamp.tv_sec, &stamp); trio_asprintf(&stmt, "insert into events(jobid,event,code,prog,host,time_stamp,usec,arrived,level,userid) " "values ('%|Ss',%d,%d,'%|Ss','%|Ss',%s,%d,%s,%d,'%|Ss')", jobid,next, - ctx->use_transactions ? (int) e->any.type : EDG_WLL_EVENT_UNDEF, + ctx->dbcaps & GLITE_LBU_DB_CAP_TRANSACTIONS ? (int) e->any.type : EDG_WLL_EVENT_UNDEF, ssrc,e->any.host, - edg_wll_TimeToDB(e->any.timestamp.tv_sec),e->any.timestamp.tv_usec, + stamp,e->any.timestamp.tv_usec, now_s, e->any.level,userid); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { if ((err = edg_wll_Error(ctx,NULL,NULL)) != EEXIST) goto clean; } else break; /* successful insert */ @@ -161,14 +162,14 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) edg_wll_SetError(ctx,oerr,desc); free(desc); } else - if (!ctx->use_transactions) { + if (!(ctx->dbcaps & GLITE_LBU_DB_CAP_TRANSACTIONS)) { /* emulate commit, i.e. swith to a real event type to make * the record valid */ trio_asprintf(&stmt, "update events set code=%d " "where jobid='%|Ss' and event=%d", (int) e->any.type,jobid,next); - switch (edg_wll_ExecStmt(ctx,stmt,NULL)) { + switch (edg_wll_ExecSQL(ctx,stmt,NULL)) { case 0: if (ctx->strict_locking) err = edg_wll_SetError(ctx,ENOENT,"event disappeared on store while strict locking"); /* purge in progres: drop the garbage, ignore errors */ @@ -186,7 +187,7 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) trio_asprintf(&stmt, "select 'x' from jobs where jobid='%|Ss'", jobid); - switch (edg_wll_ExecStmt(ctx,stmt,NULL)) { + switch (edg_wll_ExecSQL(ctx,stmt,NULL)) { case 1: break; case 0: /* purge in progres */ edg_wll_delete_event(ctx,jobid,next); @@ -205,7 +206,7 @@ int edg_wll_StoreEvent(edg_wll_Context ctx,edg_wll_Event *e,int *seq) "more event records, what is that?"); break; } - } /* if !ctx->use_transactions */ + } /* if !transactions */ if (err == 0 && e->any.type == EDG_WLL_EVENT_REGJOB && @@ -228,7 +229,7 @@ clean: free(stmt); free(ssrc); free(select_max); - if (sh) edg_wll_FreeStmt(&sh); + if (sh) glite_lbu_FreeStmt(&sh); if (!err && seq) *seq = next; return err; } @@ -240,7 +241,7 @@ static int store_user(edg_wll_Context ctx,const char *userid,const char *subj) trio_asprintf(&stmt,"insert into users(userid,cert_subj) " "values ('%|Ss','%|Ss')",userid,subj); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { if (edg_wll_Error(ctx,NULL,NULL) == EEXIST) edg_wll_ResetError(ctx); } @@ -283,7 +284,7 @@ static int store_job(edg_wll_Context ctx,const edg_wlc_JobId job,const char *use trio_asprintf(&stmt,"insert into jobs(jobid,dg_jobid,userid,proxy,server) " "values ('%|Ss','%|Ss','%|Ss', '%|Sd', '%|Sd')",jobid,jobstr,userid,proxy,server); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { if (edg_wll_Error(ctx,NULL,NULL) == EEXIST) edg_wll_ResetError(ctx); } @@ -291,7 +292,7 @@ static int store_job(edg_wll_Context ctx,const edg_wlc_JobId job,const char *use if (ctx->greyjobs) { trio_asprintf(&stmt,"delete from grey_jobs where jobid = '%|Ss'",jobid); - edg_wll_ExecStmt(ctx,stmt,NULL); /* XXX: error propagates */ + edg_wll_ExecSQL(ctx,stmt,NULL); /* XXX: error propagates */ free(stmt); } @@ -301,7 +302,7 @@ static int store_job(edg_wll_Context ctx,const edg_wlc_JobId job,const char *use } #ifdef LB_BUF -static int store_job_block(edg_wll_Context ctx,const edg_wlc_JobId job,const char *userid, edg_wll_bufInsert *bi, int proxy, int server) +static int store_job_block(edg_wll_Context ctx,const edg_wlc_JobId job,const char *userid, glite_lbu_bufInsert *bi, int proxy, int server) { char *jobstr = edg_wlc_JobIdUnparse(job); char *jobid = edg_wlc_JobIdGetUnique(job); @@ -330,17 +331,19 @@ static int store_job_grey(edg_wll_Context ctx,const edg_wlc_JobId job,time_t eti { char *jobstr = edg_wlc_JobIdUnparse(job); char *jobid = edg_wlc_JobIdGetUnique(job); - char *stmt; + char *stmt, *s_etime; if (jobid == NULL || jobstr == NULL) return edg_wll_SetError(ctx,EINVAL,"store_job_grey()"); edg_wll_ResetError(ctx); + glite_lbu_TimeToDB(etime, &s_etime); trio_asprintf(&stmt,"insert into grey_jobs(jobid,dg_jobid,time_stamp) " "values ('%|Ss','%|Ss',%s)", - jobid,jobstr,edg_wll_TimeToDB(etime)); + jobid,jobstr,s_etime); + free(s_etime); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) { if (edg_wll_Error(ctx,NULL,NULL) == EEXIST) edg_wll_ResetError(ctx); } @@ -356,7 +359,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI { char *unique = edg_wlc_JobIdGetUnique(event->any.jobId); char *q = NULL, *owner = NULL, *userid = NULL; - edg_wll_Stmt stmt = NULL; + glite_lbu_Statement stmt = NULL; int nar; @@ -372,7 +375,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI trio_asprintf(&q,"update jobs set proxy=1 where jobid='%|Ss'", unique); - nar = edg_wll_ExecStmt(ctx, q, NULL); + nar = edg_wll_ExecSQL(ctx, q, NULL); if (nar == 0) { /* job isn't registered yet */ @@ -410,7 +413,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI trio_asprintf(&q,"update jobs set jobid='%|Ss' where jobid='%|Ss'", unique, unique); /* does the job exists ? */ - if (edg_wll_ExecStmt(ctx,q,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,q,NULL) < 0) { edg_wll_SetError(ctx, ENOENT, "job not registered"); goto err; } @@ -433,7 +436,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI trio_asprintf(&q,"update jobs set jobid='%|Ss' where jobid='%|Ss'", unique, unique); /* does the job exists ? now we require registration on proxy too */ - if (edg_wll_ExecStmt(ctx,q,NULL) < 0) { + if (edg_wll_ExecSQL(ctx,q,NULL) < 0) { edg_wll_SetError(ctx, ENOENT, "job not registered"); goto err; } @@ -444,7 +447,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI if (event->any.type == EDG_WLL_EVENT_REGJOB) { trio_asprintf(&q,"select cert_subj from jobs,users where jobs.jobid='%|Ss'" " AND jobs.userid=users.userid",unique); - if ( (nar = edg_wll_ExecStmt(ctx,q,&stmt)) < 0 || edg_wll_FetchRow(stmt,&owner) < 0 ) + if ( (nar = edg_wll_ExecSQL(ctx,q,&stmt)) < 0 || edg_wll_FetchRow(ctx,stmt,1,NULL,&owner) < 0 ) goto err; if (nar) { @@ -457,7 +460,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI trio_asprintf(&q,"update jobs set server=1, userid='%|Ss' where jobid='%|Ss'", userid, unique); - edg_wll_ExecStmt(ctx, q, NULL); + edg_wll_ExecSQL(ctx, q, NULL); } else { } /* re-registration, no action needed */ } @@ -476,7 +479,7 @@ int store_job_server_proxy(edg_wll_Context ctx, edg_wll_Event *event, char *jobI } err: - if (stmt) edg_wll_FreeStmt(&stmt); + if (stmt) glite_lbu_FreeStmt(&stmt); free(unique); free(userid); free(q); @@ -503,7 +506,7 @@ static int store_seq(edg_wll_Context ctx,edg_wll_Event *e,int no) "values ('%|Ss',%d,'SEQCODE','%|Ss')", jobid,no,e->any.seqcode); - ret = edg_wll_ExecStmt(ctx,stmt,NULL); + ret = edg_wll_ExecSQL(ctx,stmt,NULL); free(stmt); free(jobid); @@ -569,7 +572,7 @@ static int store_flesh(edg_wll_Context ctx,edg_wll_Event *e,char *jobid,int no) strlen(f[i].val) <= SHORT_LEN ? "short_fields" : "long_fields", jobid,no,f[i].key,f[i].val); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) err = edg_wll_Error(ctx,NULL,NULL); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) err = edg_wll_Error(ctx,NULL,NULL); free(stmt); } for (i=0; iany.src_instance); - if (edg_wll_ExecStmt(ctx,stmt,NULL) < 0) err = edg_wll_Error(ctx,NULL,NULL); + if (edg_wll_ExecSQL(ctx,stmt,NULL) < 0) err = edg_wll_Error(ctx,NULL,NULL); free(stmt); } @@ -658,15 +661,15 @@ static int check_auth(edg_wll_Context ctx,edg_wll_Event *e) { char *jobid = edg_wlc_JobIdGetUnique(e->any.jobId); char *q = NULL,*owner = NULL; - edg_wll_Stmt stmt = NULL; + glite_lbu_Statement stmt = NULL; char *user; edg_wll_ResetError(ctx); trio_asprintf(&q,"select userid from jobs where jobid='%|Ss'",jobid); - if (edg_wll_ExecStmt(ctx,q,&stmt) < 0 - || edg_wll_FetchRow(stmt,&owner) < 0 + if (edg_wll_ExecSQL(ctx,q,&stmt) < 0 + || edg_wll_FetchRow(ctx,stmt,1,NULL,&owner) < 0 ) goto clean; if (!owner) { @@ -697,7 +700,7 @@ static int check_auth(edg_wll_Context ctx,edg_wll_Event *e) clean: - if (stmt) edg_wll_FreeStmt(&stmt); + if (stmt) glite_lbu_FreeStmt(&stmt); free(q); free(owner); return edg_wll_Error(ctx,NULL,NULL); @@ -850,11 +853,11 @@ static int register_subjobs_embryonic(edg_wll_Context ctx,const edg_wll_RegJobEv char *jobid_md5, *jobid_md5_old; size_t jobid_len; #ifdef LB_BUF - edg_wll_bufInsert bi_j; - edg_wll_bufInsert *bi_jobs = &bi_j; + glite_lbu_bufInsert bi_j; + glite_lbu_bufInsert *bi_jobs = &bi_j; char *states_cols; #endif - edg_wll_bufInsert bi_s, *bi_states = &bi_s; + glite_lbu_bufInsert bi_s, *bi_states = &bi_s; char *icnames, *values; int server, proxy, membership = 0; @@ -971,19 +974,19 @@ int edg_wll_delete_event(edg_wll_Context ctx,const char *jobid,int event) trio_asprintf(&stmt, "delete from short_fields where jobid='%|Ss' and event=%d", jobid,event); - edg_wll_ExecStmt(ctx,stmt,NULL); + edg_wll_ExecSQL(ctx,stmt,NULL); free(stmt); trio_asprintf(&stmt, "delete from long_fields where jobid='%|Ss' and event=%d", jobid,event); - edg_wll_ExecStmt(ctx,stmt,NULL); + edg_wll_ExecSQL(ctx,stmt,NULL); free(stmt); trio_asprintf(&stmt, "delete from events where jobid='%|Ss' and event=%d", jobid,event); - edg_wll_ExecStmt(ctx,stmt,NULL); + edg_wll_ExecSQL(ctx,stmt,NULL); free (stmt); return edg_wll_Error(ctx,NULL,NULL); diff --git a/org.glite.lb.server/src/userjobs.c b/org.glite.lb.server/src/userjobs.c index 5ce6f15..5671d82 100644 --- a/org.glite.lb.server/src/userjobs.c +++ b/org.glite.lb.server/src/userjobs.c @@ -10,7 +10,8 @@ #include "glite/lbu/trio.h" #include "glite/lb/context-int.h" -#include "lbs_db.h" +#include "jobstat.h" +#include "db_supp.h" int edg_wll_UserJobs( edg_wll_Context ctx, @@ -21,30 +22,30 @@ int edg_wll_UserJobs( *res = NULL; int njobs = 0,ret,i,j; edg_wlc_JobId *out = NULL; - edg_wll_Stmt sth = NULL; + glite_lbu_Statement sth = NULL; edg_wll_ErrorCode err = 0; edg_wll_ResetError(ctx); trio_asprintf(&stmt,"select cert_subj from users where userid = '%|Ss'",userid); - switch (edg_wll_ExecStmt(ctx,stmt,&sth)) { + switch (edg_wll_ExecSQL(ctx,stmt,&sth)) { case 0: edg_wll_SetError(ctx,ENOENT,ctx->peerName); case -1: goto err; default: - if (edg_wll_FetchRow(sth,&res) < 0) goto err; + if (edg_wll_FetchRow(ctx,sth,1,NULL,&res) < 0) goto err; if (strcmp(ctx->peerName,res)) { edg_wll_SetError(ctx,EDG_WLL_ERROR_MD5_CLASH,ctx->peerName); goto err; } } - edg_wll_FreeStmt(&sth); + glite_lbu_FreeStmt(&sth); free(stmt); stmt = NULL; free(res); res = NULL; trio_asprintf(&stmt,"select dg_jobid from jobs where userid = '%|Ss'",userid); - switch (njobs = edg_wll_ExecStmt(ctx,stmt,&sth)) { + switch (njobs = edg_wll_ExecSQL(ctx,stmt,&sth)) { case 0: edg_wll_SetError(ctx,ENOENT,ctx->peerName); case -1: goto err; } @@ -52,7 +53,7 @@ int edg_wll_UserJobs( out = malloc(sizeof(*out)*(njobs+1)); memset(out,0,sizeof(*out)*(njobs+1)); - for (i=0; (ret = edg_wll_FetchRow(sth,&res)); i++) { + for (i=0; (ret = edg_wll_FetchRow(ctx,sth,1,NULL,&res)); i++) { if (ret < 0) goto err; if ((ret = edg_wlc_JobIdParse(res,out+i))) { edg_wll_SetError(ctx,errno,res); @@ -72,7 +73,7 @@ int edg_wll_UserJobs( err: free(res); free(stmt); - edg_wll_FreeStmt(&sth); + glite_lbu_FreeStmt(&sth); if ((err = edg_wll_Error(ctx,NULL,NULL))) { if (out) { for (i=0; i #include +#include #include #include -#include "lbs_db.h" #include "get_events.h" using namespace std; @@ -30,13 +30,17 @@ private: public: void oneJob(); - int ExecStmt(const char *, edg_wll_Stmt *); + int ExecStmt(const char *, glite_lbu_Statement *); void setUp() { edg_wll_InitContext(&ctx); - ctx->mysql = (void *) this; /* XXX */ + ctx->dbctx = (glite_lbu_DBContext) this; /* XXX */ + ctx->dbcaps = 0; } + void tearDown() { + edg_wll_FreeContext(ctx); + } }; void QueryEventsTest::oneJob() @@ -44,6 +48,7 @@ void QueryEventsTest::oneJob() edg_wll_QueryRec job[2]; const edg_wll_QueryRec *jobs[2] = { job,NULL} ; edg_wll_Event *events; + int i; job[0].attr = EDG_WLL_QUERY_ATTR_JOBID; job[0].op = EDG_WLL_QUERY_OP_EQUAL ; @@ -76,9 +81,12 @@ void QueryEventsTest::oneJob() qry_file.close(); CPPUNIT_ASSERT(!edg_wll_QueryEventsServer(ctx,1,jobs,NULL,&events)); + edg_wlc_JobIdFree(job[0].value.j); + for (i = 0; events[i].type; i++) edg_wll_FreeEvent(&events[i]); + free(events); } -int QueryEventsTest::ExecStmt(const char *qry, edg_wll_Stmt *stmt_out) +int QueryEventsTest::ExecStmt(const char *qry, glite_lbu_Statement *stmt_out) { vector > >::iterator stmt = queries.begin(); @@ -95,21 +103,27 @@ int QueryEventsTest::ExecStmt(const char *qry, edg_wll_Stmt *stmt_out) } vector::iterator *rows = new vector::iterator(stmt->second.begin()); - *stmt_out = (edg_wll_Stmt) rows; + *stmt_out = (glite_lbu_Statement) rows; +//cerr << (*rows)->c_str() << endl; +//cerr << stmt->second.size()-1 << endl; return stmt->second.size()-1; } extern "C" { +int glite_lbu_InitDBContext(glite_lbu_DBContext *ctx) { return 0; } +void glite_lbu_FreeDBContext(glite_lbu_DBContext ctx) { } +int glite_lbu_DBConnect(glite_lbu_DBContext ctx, const char*str) { return 0; } +void glite_lbu_DBClose(glite_lbu_DBContext ctx) { } -int edg_wll_ExecStmt(edg_wll_Context ctx,char *qry,edg_wll_Stmt *stmt) +int glite_lbu_ExecSQL(glite_lbu_DBContext ctx,const char *qry,glite_lbu_Statement *stmt) { - cout << "edg_wll_ExecStmt: " << qry << endl; + cout << "glite_lbu_ExecSQL: " << qry << endl; - class QueryEventsTest *tst = (class QueryEventsTest *)(ctx->mysql); + class QueryEventsTest *tst = (class QueryEventsTest *)ctx; return tst->ExecStmt(qry, stmt); } -int edg_wll_FetchRow(edg_wll_Stmt stmt, char **cols) +int glite_lbu_FetchRow(glite_lbu_Statement stmt, unsigned int n, unsigned long int *lengths, char **cols) { vector::iterator *rows = (vector::iterator *) stmt; char *row,*p,i=0; @@ -119,28 +133,28 @@ int edg_wll_FetchRow(edg_wll_Stmt stmt, char **cols) (*rows)++; for (p = strtok(row,"\t"); p; p = strtok(NULL,"\t")) cols[i++] = strdup(p); + free(row); return i; } -void edg_wll_FreeStmt(edg_wll_Stmt *) {} +void glite_lbu_FreeStmt(glite_lbu_Statement *) {} int debug; -int edg_wll_QueryColumns(edg_wll_Stmt stmt, char**cols) { return 0; } -char *edg_wll_TimeToDB(long t) { return NULL; } - -time_t edg_wll_DBToTime(char *c) { return (time_t)-1; } -edg_wll_ErrorCode edg_wll_DBConnect(edg_wll_Context ctx, const char*str) { - return (edg_wll_ErrorCode)0; -} +int glite_lbu_QueryColumns(glite_lbu_Statement stmt, char**cols) { return 0; } +void glite_lbu_TimeToDB(long t, char **s) { *s = NULL; } +time_t glite_lbu_DBToTime(const char *c) { return (time_t)-1; } -int edg_wll_Transaction(edg_wll_Context ctx) { return 0; } -int edg_wll_Commit(edg_wll_Context ctx) { return 0; } -int edg_wll_Rollback(edg_wll_Context ctx) { return 0; } +int glite_lbu_Transaction(glite_lbu_DBContext ctx) { return 0; } +int glite_lbu_Commit(glite_lbu_DBContext ctx) { return 0; } +int glite_lbu_Rollback(glite_lbu_DBContext ctx) { return 0; } -edg_wll_ErrorCode edg_wll_bufferedInsert(edg_wll_bufInsert *bi, char *row) { return (edg_wll_ErrorCode) 0; }; - +int glite_lbu_bufferedInsertInit(glite_lbu_DBContext ctx, glite_lbu_bufInsert *bi, const char *table_name, long size_limit, long record_limit, const char * columns) { return 0; } +int glite_lbu_bufferedInsert(glite_lbu_bufInsert bi, const char *row) { return 0; } +int glite_lbu_bufferedInsertClose(glite_lbu_bufInsert bi) { return 0; } +int glite_lbu_QueryIndices(glite_lbu_DBContext ctx, const char *table, char ***key_names, char ****column_names) { return 0; } +int glite_lbu_DBError(glite_lbu_DBContext ctx, char **s1, char **s2) { return 0; } } CPPUNIT_TEST_SUITE_REGISTRATION(QueryEventsTest); diff --git a/org.glite.lbjp-common.db/interface/db.h b/org.glite.lbjp-common.db/interface/db.h index f28e2b8..233c8c3 100644 --- a/org.glite.lbjp-common.db/interface/db.h +++ b/org.glite.lbjp-common.db/interface/db.h @@ -333,6 +333,12 @@ int glite_lbu_ExecPreparedStmt(glite_lbu_Statement stmt, int n, ...); /** + * Returns the last automatically generated id, if any. + */ +long int glite_lbu_Lastid(glite_lbu_Statement stmt); + + +/** * @} database group */ diff --git a/org.glite.lbjp-common.db/src/db.c b/org.glite.lbjp-common.db/src/db.c index a3fab13..c9b21b7 100644 --- a/org.glite.lbjp-common.db/src/db.c +++ b/org.glite.lbjp-common.db/src/db.c @@ -110,8 +110,7 @@ static void db_close(MYSQL *mysql); static int transaction_test(glite_lbu_DBContext ctx); static int FetchRowSimple(glite_lbu_DBContext ctx, MYSQL_RES *result, unsigned long *lengths, char **results); static int FetchRowPrepared(glite_lbu_DBContext ctx, glite_lbu_Statement stmt, unsigned int n, unsigned long *lengths, char **results); -void set_time(MYSQL_TIME *mtime, const time_t time); -time_t get_time(const MYSQL_TIME *mtime); +static void set_time(MYSQL_TIME *mtime, const time_t time); /* ---- common ---- */ @@ -152,6 +151,7 @@ int glite_lbu_DBConnect(glite_lbu_DBContext ctx, const char *cs) { void glite_lbu_DBClose(glite_lbu_DBContext ctx) { db_close(ctx->mysql); ctx->mysql = NULL; + CLR_ERR(ctx); } @@ -187,6 +187,7 @@ void glite_lbu_DBSetCaps(glite_lbu_DBContext ctx, int caps) { int glite_lbu_Transaction(glite_lbu_DBContext ctx) { + CLR_ERR(ctx); if (USE_TRANS(ctx)) { if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=0", NULL) < 0) goto err; if (glite_lbu_ExecSQL(ctx, "BEGIN", NULL) < 0) goto err; @@ -197,6 +198,7 @@ err: int glite_lbu_Commit(glite_lbu_DBContext ctx) { + CLR_ERR(ctx); if (USE_TRANS(ctx)) { if (glite_lbu_ExecSQL(ctx, "COMMIT", NULL) < 0) goto err; if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=1", NULL) < 0) goto err; @@ -207,6 +209,7 @@ err: int glite_lbu_Rollback(glite_lbu_DBContext ctx) { + CLR_ERR(ctx); if (USE_TRANS(ctx)) { if (glite_lbu_ExecSQL(ctx, "ROLLBACK", NULL) < 0) goto err; if (glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=1", NULL) < 0) goto err; @@ -239,7 +242,7 @@ int glite_lbu_QueryIndices(glite_lbu_DBContext ctx, const char *table, char ***k int i,j,ret; /* XXX: "show index from" columns. Matches at least MySQL 4.0.11 */ - char *showcol[12]; + char *sql, *showcol[12]; int Key_name,Seq_in_index,Column_name,Sub_part; char **keys = NULL; @@ -252,8 +255,12 @@ int glite_lbu_QueryIndices(glite_lbu_DBContext ctx, const char *table, char ***k Key_name = Seq_in_index = Column_name = Sub_part = -1; - if (glite_lbu_ExecSQL(ctx,"show index from states",&stmt)<0) + asprintf(&sql, "show index from %s", table); + if (glite_lbu_ExecSQL(ctx,sql,&stmt)<0) { + free(sql); return STATUS(ctx); + } + free(sql); while ((ret = glite_lbu_FetchRow(stmt,sizeof(showcol)/sizeof(showcol[0]),NULL,showcol)) > 0) { assert(ret <= (int)(sizeof showcol/sizeof showcol[0])); @@ -413,6 +420,7 @@ int glite_lbu_QueryColumns(glite_lbu_Statement stmt, char **cols) int i = 0; MYSQL_FIELD *f; + CLR_ERR(stmt->ctx); if (!stmt->result) return ERR(stmt->ctx, EINVAL, "QueryColumns implemented only for simple API"); while ((f = mysql_fetch_field(stmt->result))) cols[i++] = f->name; return i == 0; @@ -681,6 +689,16 @@ int glite_lbu_bufferedInsertClose(glite_lbu_bufInsert bi) } +long int glite_lbu_Lastid(glite_lbu_Statement stmt) { + my_ulonglong i; + + CLR_ERR(stmt->ctx); + i = mysql_stmt_insert_id(stmt->stmt); + assert(i < ((unsigned long int)-1) >> 1); + return (long int)i; +} + + /* * helping compatibility function: clear error from the context */ @@ -726,7 +744,7 @@ static int myerrstmt(glite_lbu_Statement stmt, const char *source, int line) { /* - * Ehelping function: error handle + * helping function: error handle * * \return -1 failed * \return 0 retry @@ -746,8 +764,10 @@ static int myisokstmt(glite_lbu_Statement stmt, const char *source, int line, in if (*retry > 0) { (*retry)--; return 0; - } else + } else { + myerrstmt(stmt, source, line); return -1; + } break; default: myerrstmt(stmt, source, line); @@ -814,7 +834,7 @@ static int db_connect(glite_lbu_DBContext ctx, const char *cs, MYSQL **mysql) { free(buf); ctx->cs = cs; - return 0; + return CLR_ERR(ctx); } @@ -983,7 +1003,7 @@ quit: } -void set_time(MYSQL_TIME *mtime, const time_t time) { +static void set_time(MYSQL_TIME *mtime, const time_t time) { struct tm tm; gmtime_r(&time, &tm); @@ -995,19 +1015,3 @@ void set_time(MYSQL_TIME *mtime, const time_t time) { mtime->minute = tm.tm_min; mtime->second = tm.tm_sec; } - - -time_t get_time(const MYSQL_TIME *mtime) { - struct tm tm; - - memset(&tm, 0, sizeof(tm)); - setenv("TZ","UTC",1); tzset(); - tm.tm_year = mtime->year - 1900; - tm.tm_mon = mtime->month - 1; - tm.tm_mday = mtime->day; - tm.tm_hour = mtime->hour; - tm.tm_min = mtime->minute; - tm.tm_sec = mtime->second; - - return mktime(&tm); -} -- 1.8.2.3