DB module:
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Fri, 1 Feb 2008 17:54:58 +0000 (17:54 +0000)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Fri, 1 Feb 2008 17:54:58 +0000 (17:54 +0000)
 - force autocommit to 1 on begin
 - set transaction isolation level
 - fixed transaction test
BK server:
 - checks of DB schema

org.glite.lb.server/src/openserver.c
org.glite.lbjp-common.db/src/db.c

index 9b043d5..78a1658 100644 (file)
@@ -1,11 +1,20 @@
 #ident "$Header$"
 
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
 #include "glite/lb/context-int.h"
 #include "openserver.h"
 #include "db_supp.h"
 
 edg_wll_ErrorCode edg_wll_Open(edg_wll_Context ctx, char *cs)
 {
+       int ret, hit = 0, i;
+       char *table[1];
+       char *cols[20];
+       glite_lbu_Statement stmt;
+
        if (glite_lbu_InitDBContext(&ctx->dbctx) != 0) {
                char *ed;
 
@@ -14,7 +23,43 @@ edg_wll_ErrorCode edg_wll_Open(edg_wll_Context ctx, char *cs)
                free(ed);
                return EDG_WLL_ERROR_DB_INIT;
        }
-       return glite_lbu_DBConnect(ctx->dbctx,cs) ? edg_wll_SetErrorDB(ctx) : 0;
+       if (glite_lbu_DBConnect(ctx->dbctx,cs) != 0) return edg_wll_SetErrorDB(ctx);
+
+       // proxy and server columns added
+       if (glite_lbu_ExecSQL(ctx->dbctx, "DESC jobs", &stmt) <= 0) goto err;
+       hit = 0;
+       while (hit < 2 && (ret = glite_lbu_FetchRow(stmt, 1, NULL, cols)) > 0) {
+               assert(ret <= (int)(sizeof cols/sizeof cols[0]));
+               if (strcasecmp(cols[0], "proxy") == 0 || 
+                   strcasecmp(cols[0], "server") == 0) hit++;
+               for (i = 0; i < ret; i++) free(cols[i]);
+       }
+       if (ret < 0) goto err;
+       if (hit != 2) {
+               ret = edg_wll_SetError(ctx, EINVAL, "old DB schema found, migration to new schema needed");
+               goto close_db;
+       }
+
+       // events_flesh table added
+       if (glite_lbu_ExecSQL(ctx->dbctx, "SHOW TABLES", &stmt) <= 0) goto err;
+       hit = 0;
+       while (hit < 1 && (ret = glite_lbu_FetchRow(stmt, 1, NULL, table)) > 0) {
+               if (strcasecmp(table[0], "events_flesh") == 0) hit++;
+               free(table[0]);
+       }
+       if (ret < 0) goto err;
+       if (hit != 1) {
+               ret = edg_wll_SetError(ctx, EINVAL, "events_flesh table not found, migration to new schema needed");
+               goto close_db;
+       }
+
+       return 0;
+
+err:
+       edg_wll_SetErrorDB(ctx);
+close_db:
+       glite_lbu_DBClose(ctx->dbctx);
+       return ret;
 }
 
 edg_wll_ErrorCode edg_wll_Close(edg_wll_Context ctx)
index a45a16c..43dfc4b 100644 (file)
@@ -256,8 +256,12 @@ void glite_lbu_FreeDBContext(glite_lbu_DBContext ctx) {
 
 
 int glite_lbu_DBConnect(glite_lbu_DBContext ctx, const char *cs) {
-       if (db_connect(ctx, cs, &ctx->mysql) != 0) return STATUS(ctx);
-       return 0;
+       if (db_connect(ctx, cs, &ctx->mysql) != 0 ||
+           glite_lbu_ExecSQL(ctx, "SET AUTOCOMMIT=1", NULL) < 0 ||
+           glite_lbu_ExecSQL(ctx, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ", NULL) < 0)
+               return STATUS(ctx);
+       else
+               return 0;
 }
 
 
@@ -1022,8 +1026,15 @@ static int transaction_test(glite_lbu_DBContext ctx) {
 
        trio_asprintf(&cmd, "SHOW CREATE TABLE %|Ss", table[0]);
        if (glite_lbu_ExecSQL(ctx, cmd, &stmt) <= 0 || (retval = glite_lbu_FetchRow(stmt, 2, NULL, res)) < 0 ) goto quit;
-       if (retval != 2 || strcmp(res[0], table[0])) ERR(ctx, EIO, "unexpected show create result");
-       else ctx->caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
+       if (retval != 2 || strcmp(res[0], table[0])) {
+               ERR(ctx, EIO, "unexpected show create result");
+               goto quit;
+       }
+
+       if (glite_lbu_ExecSQL(ctx, "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ", NULL) <= 0) goto quit;
+
+       if (strstr(res[1],"ENGINE=InnoDB"))
+               ctx->caps |= GLITE_LBU_DB_CAP_TRANSACTIONS;
 
 #ifdef LBS_DB_PROFILE
        fprintf(stderr, "[%d] use_transactions = %d\n", getpid(), USE_TRANS(ctx));
@@ -1031,10 +1042,10 @@ static int transaction_test(glite_lbu_DBContext ctx) {
 
 quit:
        glite_lbu_FreeStmt(&stmt);
-       if (table[0]) free(table[0]);
-       if (res[0]) free(res[0]);
-       if (res[1]) free(res[1]);
-       if (cmd) free(cmd);
+       free(table[0]);
+       free(res[0]);
+       free(res[1]);
+       free(cmd);
        return STATUS(ctx);
 }