Changeset: 0202546d48bb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/0202546d48bb
Modified Files:
        sql/backends/monet5/sql_scenario.c
        sql/server/sql_mvc.c
        sql/server/sql_mvc.h
        sql/storage/sql_storage.h
        sql/storage/store.c
Branch: Jul2021
Log Message:

Cleanup storage on failed startup


diffs (truncated from 386 to 300 lines):

diff --git a/sql/backends/monet5/sql_scenario.c 
b/sql/backends/monet5/sql_scenario.c
--- a/sql/backends/monet5/sql_scenario.c
+++ b/sql/backends/monet5/sql_scenario.c
@@ -390,7 +390,6 @@ SQLinit(Client c)
        static int maybeupgrade = 1;
        backend *be = NULL;
        mvc *m = NULL;
-       sql_allocator *sa = NULL;
        const char *opt_pipe;
 
        if ((opt_pipe = GDKgetenv("sql_optimizer")) && 
!isOptimizerPipe(opt_pipe))
@@ -417,11 +416,7 @@ SQLinit(Client c)
        if (readonly)
                SQLdebug |= 32;
 
-       if (!(sa = sa_create(NULL))) {
-               MT_lock_unset(&sql_contextLock);
-               throw(SQL,"sql.init",SQLSTATE(HY013) MAL_MALLOC_FAIL);
-       }
-       if ((SQLstore = mvc_init(sa, SQLdebug, GDKinmemory(0) ? store_mem : 
store_bat, readonly, single_user)) == NULL) {
+       if ((SQLstore = mvc_init(SQLdebug, GDKinmemory(0) ? store_mem : 
store_bat, readonly, single_user)) == NULL) {
                MT_lock_unset(&sql_contextLock);
                throw(SQL, "SQLinit", SQLSTATE(42000) "Catalogue initialization 
failed");
        }
@@ -434,6 +429,8 @@ SQLinit(Client c)
                bstream *fdin;
 
                if ( b == NULL || cbuf == NULL) {
+                       mvc_exit(SQLstore);
+                       SQLstore = NULL;
                        MT_lock_unset(&sql_contextLock);
                        GDKfree(b);
                        GDKfree(cbuf);
@@ -443,6 +440,8 @@ SQLinit(Client c)
                buffer_init(b, cbuf, len);
                buf = buffer_rastream(b, "si");
                if ( buf == NULL) {
+                       mvc_exit(SQLstore);
+                       SQLstore = NULL;
                        MT_lock_unset(&sql_contextLock);
                        buffer_destroy(b);
                        throw(SQL,"sql.init",SQLSTATE(HY013) MAL_MALLOC_FAIL);
@@ -450,6 +449,8 @@ SQLinit(Client c)
 
                fdin = bstream_create(buf, b->len);
                if ( fdin == NULL) {
+                       mvc_exit(SQLstore);
+                       SQLstore = NULL;
                        MT_lock_unset(&sql_contextLock);
                        buffer_destroy(b);
                        throw(SQL,"sql.init",SQLSTATE(HY013) MAL_MALLOC_FAIL);
@@ -460,6 +461,8 @@ SQLinit(Client c)
                        TRC_ERROR(SQL_PARSER, "Could not switch client input 
stream\n");
        }
        if ((msg = SQLprepareClient(c, 0)) != NULL) {
+               mvc_exit(SQLstore);
+               SQLstore = NULL;
                MT_lock_unset(&sql_contextLock);
                TRC_INFO(SQL_PARSER, "%s\n", msg);
                return msg;
@@ -491,7 +494,6 @@ SQLinit(Client c)
                        if (m->sa)
                                sa_destroy(m->sa);
                        m->sa = NULL;
-
                }
                /* 99_system.sql */
                if (!msg) {
@@ -553,22 +555,36 @@ SQLinit(Client c)
        }
 
        other = SQLresetClient(c);
-       MT_lock_unset(&sql_contextLock);
        if (other && !msg) /* 'msg' variable might be set or not, as well as 
'other'. Throw the earliest one */
                msg = other;
        else if (other)
                freeException(other);
-       if (msg != MAL_SUCCEED)
+       if (msg != MAL_SUCCEED) {
+               mvc_exit(SQLstore);
+               SQLstore = NULL;
+               MT_lock_unset(&sql_contextLock);
                return msg;
+       }
 
-       if (GDKinmemory(0))
-               return MAL_SUCCEED;
+       if (GDKinmemory(0)) {
+               MT_lock_unset(&sql_contextLock);
+               return msg;
+       }
 
        if ((sqllogthread = THRcreate((void (*)(void *)) mvc_logmanager, 
SQLstore, MT_THR_DETACHED, "logmanager")) == 0) {
+               mvc_exit(SQLstore);
+               SQLstore = NULL;
+               MT_lock_unset(&sql_contextLock);
                throw(SQL, "SQLinit", SQLSTATE(42000) "Starting log manager 
failed");
        }
-       if ( wlc_state == WLC_STARTUP)
-               return WLCinit();
+       if (wlc_state == WLC_STARTUP && (msg = WLCinit()) != MAL_SUCCEED) {
+               mvc_exit(SQLstore);
+               SQLstore = NULL;
+               MT_lock_unset(&sql_contextLock);
+               return msg;
+       }
+
+       MT_lock_unset(&sql_contextLock);
        return MAL_SUCCEED;
 }
 
diff --git a/sql/server/sql_mvc.c b/sql/server/sql_mvc.c
--- a/sql/server/sql_mvc.c
+++ b/sql/server/sql_mvc.c
@@ -121,7 +121,7 @@ mvc_fix_depend(mvc *m, sql_column *depid
 }
 
 sql_store
-mvc_init(sql_allocator *pa, int debug, store_type store_tpe, int ro, int su)
+mvc_init(int debug, store_type store_tpe, int ro, int su)
 {
        sqlstore *store = NULL;
        sql_schema *s;
@@ -131,18 +131,20 @@ mvc_init(sql_allocator *pa, int debug, s
 
        TRC_DEBUG(SQL_TRANS, "Initialization\n");
        keyword_init();
-       if(scanner_init_keywords() != 0) {
+       if (scanner_init_keywords() != 0) {
                TRC_CRITICAL(SQL_TRANS, "Malloc failure\n");
                return NULL;
        }
 
-       if ((store = store_init(pa, debug, store_tpe, ro, su)) == NULL) {
+       if ((store = store_init(debug, store_tpe, ro, su)) == NULL) {
+               keyword_exit();
                TRC_CRITICAL(SQL_TRANS, "Unable to create system tables\n");
                return NULL;
        }
 
-       m = mvc_create((sql_store)store, pa, 0, 0, NULL, NULL);
+       m = mvc_create((sql_store)store, store->sa, 0, 0, NULL, NULL);
        if (!m) {
+               mvc_exit(store);
                TRC_CRITICAL(SQL_TRANS, "Malloc failure\n");
                return NULL;
        }
@@ -151,6 +153,7 @@ mvc_init(sql_allocator *pa, int debug, s
        m->sa = sa_create(m->pa);
        if (!m->sa) {
                mvc_destroy(m);
+               mvc_exit(store);
                TRC_CRITICAL(SQL_TRANS, "Malloc failure\n");
                return NULL;
        }
@@ -263,6 +266,7 @@ mvc_init(sql_allocator *pa, int debug, s
                };
                if (mvc_trans(m) < 0) {
                        mvc_destroy(m);
+                       mvc_exit(store);
                        TRC_CRITICAL(SQL_TRANS, "Failed to start 
transaction\n");
                        return NULL;
                }
@@ -277,6 +281,7 @@ mvc_init(sql_allocator *pa, int debug, s
                t = mvc_init_create_view(m, s, "tables", "SELECT \"id\", 
\"name\", \"schema_id\", \"query\", CAST(CASE WHEN \"system\" THEN \"type\" + 
10 /* system table/view */ ELSE (CASE WHEN \"commit_action\" = 0 THEN \"type\" 
/* table/view */ ELSE \"type\" + 20 /* global temp table */ END) END AS 
SMALLINT) AS \"type\", \"system\", \"commit_action\", \"access\", CASE WHEN 
(NOT \"system\" AND \"commit_action\" > 0) THEN 1 ELSE 0 END AS \"temporary\" 
FROM \"sys\".\"_tables\" WHERE \"type\" <> 2 UNION ALL SELECT \"id\", \"name\", 
\"schema_id\", \"query\", CAST(\"type\" + 30 /* local temp table */ AS 
SMALLINT) AS \"type\", \"system\", \"commit_action\", \"access\", 1 AS 
\"temporary\" FROM \"tmp\".\"_tables\";");
                if (!t) {
                        mvc_destroy(m);
+                       mvc_exit(store);
                        TRC_CRITICAL(SQL_TRANS, "Failed to create 'tables' 
view\n");
                        return NULL;
                }
@@ -288,6 +293,7 @@ mvc_init(sql_allocator *pa, int debug, s
                                                             tview[i].digits);
                        if (col == NULL) {
                                mvc_destroy(m);
+                               mvc_exit(store);
                                TRC_CRITICAL(SQL_TRANS,
                                             "Initialization: creation of 
sys.tables column %s failed\n", tview[i].name);
                                return NULL;
@@ -312,6 +318,7 @@ mvc_init(sql_allocator *pa, int debug, s
                t = mvc_init_create_view(m, s, "columns", "SELECT * FROM 
(SELECT p.* FROM \"sys\".\"_columns\" AS p UNION ALL SELECT t.* FROM 
\"tmp\".\"_columns\" AS t) AS columns;");
                if (!t) {
                        mvc_destroy(m);
+                       mvc_exit(store);
                        TRC_CRITICAL(SQL_TRANS, "Failed to create 'columns' 
view\n");
                        return NULL;
                }
@@ -322,6 +329,7 @@ mvc_init(sql_allocator *pa, int debug, s
                                                             cview[i].digits);
                        if (col == NULL) {
                                mvc_destroy(m);
+                               mvc_exit(store);
                                TRC_CRITICAL(SQL_TRANS,
                                             "Initialization: creation of 
sys.tables column %s failed\n", cview[i].name);
                                return NULL;
@@ -350,12 +358,15 @@ mvc_init(sql_allocator *pa, int debug, s
                if ((msg = mvc_commit(m, 0, NULL, false)) != MAL_SUCCEED) {
                        TRC_CRITICAL(SQL_TRANS, "Unable to commit system 
tables: %s\n", (msg + 6));
                        freeException(msg);
+                       mvc_destroy(m);
+                       mvc_exit(store);
                        return NULL;
                }
        }
 
        if (mvc_trans(m) < 0) {
                mvc_destroy(m);
+               mvc_exit(store);
                TRC_CRITICAL(SQL_TRANS, "Failed to start transaction\n");
                return NULL;
        }
@@ -375,6 +386,8 @@ mvc_init(sql_allocator *pa, int debug, s
                                if ((err = initialize_sql_parts(m, tt)) != 
NULL) {
                                        TRC_CRITICAL(SQL_TRANS, "Unable to 
start partitioned table: %s.%s: %s\n", ss->base.name, tt->base.name, err);
                                        freeException(err);
+                                       mvc_destroy(m);
+                                       mvc_exit(store);
                                        return NULL;
                                }
                        }
@@ -384,6 +397,8 @@ mvc_init(sql_allocator *pa, int debug, s
        if ((msg = mvc_commit(m, 0, NULL, false)) != MAL_SUCCEED) {
                TRC_CRITICAL(SQL_TRANS, "Unable to commit system tables: %s\n", 
(msg + 6));
                freeException(msg);
+               mvc_destroy(m);
+               mvc_exit(store);
                return NULL;
        }
 
diff --git a/sql/server/sql_mvc.h b/sql/server/sql_mvc.h
--- a/sql/server/sql_mvc.h
+++ b/sql/server/sql_mvc.h
@@ -155,7 +155,7 @@ typedef struct mvc {
 extern sql_table *mvc_init_create_view(mvc *sql, sql_schema *s, const char 
*name, const char *query);
 
 /* should return structure */
-extern sql_store mvc_init(sql_allocator *pa, int debug, store_type store, int 
ro, int su);
+extern sql_store mvc_init(int debug, store_type store, int ro, int su);
 extern void mvc_exit(sql_store store);
 
 extern void mvc_logmanager(sql_store store);
diff --git a/sql/storage/sql_storage.h b/sql/storage/sql_storage.h
--- a/sql/storage/sql_storage.h
+++ b/sql/storage/sql_storage.h
@@ -309,7 +309,7 @@ extern res_table *res_tables_remove(res_
 sql_export void res_tables_destroy(res_table *results);
 extern res_table *res_tables_find(res_table *results, int res_id);
 
-extern struct sqlstore *store_init(sql_allocator *pa, int debug, store_type 
store, int readonly, int singleuser);
+extern struct sqlstore *store_init(int debug, store_type store, int readonly, 
int singleuser);
 extern void store_exit(struct sqlstore *store);
 
 extern void store_suspend_log(struct sqlstore *store);
diff --git a/sql/storage/store.c b/sql/storage/store.c
--- a/sql/storage/store.c
+++ b/sql/storage/store.c
@@ -1733,7 +1733,6 @@ store_load(sqlstore *store, sql_allocato
        sql_trans *tr;
        sql_table *t, *types, *functions, *arguments;
        sql_schema *s;
-
        lng lng_store_oid;
 
        store->sa = pa;
@@ -1751,8 +1750,10 @@ store_load(sqlstore *store, sql_allocato
        if (!sequences_init())
                return NULL;
        tr = sql_trans_create(store, NULL, NULL);
-       if (!tr)
+       if (!tr) {
+               TRC_CRITICAL(SQL_STORE, "Failed to start a transaction while 
loading the storage\n");
                return NULL;
+       }
        tr->store = store;
 
        /* for now use malloc and free */
@@ -1760,14 +1761,10 @@ store_load(sqlstore *store, sql_allocato
        store->dependencies = hash_new(NULL, 32, (fkeyvalue)&dep_hash);
        store->depchanges = hash_new(NULL, 32, (fkeyvalue)&dep_hash);
 
-       if (store->first) {
+       if (store->first && store->readonly) {
                /* cannot initialize database in readonly mode */
-               if (store->readonly)
-                       return NULL;
-               if (!tr) {
-                       TRC_CRITICAL(SQL_STORE, "Failed to start a transaction 
while loading the storage\n");
-                       return NULL;
-               }
+               sql_trans_destroy(tr);
+               return NULL;
        }
        tr->active = 1;
 
@@ -1973,13 +1970,15 @@ store_load(sqlstore *store, sql_allocato
                insert_types(tr, types);
                insert_functions(tr, functions, funcs, arguments);
                insert_schemas(tr);
-
        } else {
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to