]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
Changed the way we do the TSD area.
[citadel.git] / citadel / database_sleepycat.c
index c5d6c4b8e4ad50c5749477e8a1f287aa794c531d..64191dcb940ef6bd937d8c3edbe8312dfffce914 100644 (file)
 #include <pthread.h>
 #include "citadel.h"
 #include "server.h"
-#include "serv_extensions.h"
 #include "citserver.h"
 #include "database.h"
 #include "msgbase.h"
 #include "sysdep_decls.h"
 #include "config.h"
 
+#include "ctdl_module.h"
+
+
 static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
-struct cdbtsd {                        /* Thread-specific DB stuff */
-       DB_TXN *tid;            /* Transaction handle */
-       DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
-};
 
 #ifdef HAVE_ZLIB
 #include <zlib.h>
 #endif
 
-static pthread_key_t tsdkey;
-
-#define MYCURSORS      (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
-#define MYTID          (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
 
 /* Verbose logging callback */
 void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
@@ -155,10 +149,10 @@ static void bailIfCursor(DBC ** cursors, const char *msg)
                }
 }
 
-static void check_handles(void *arg)
+void check_handles(void *arg)
 {
        if (arg != NULL) {
-               struct cdbtsd *tsd = (struct cdbtsd *) arg;
+               ThreadTSD *tsd = (ThreadTSD *) arg;
 
                bailIfCursor(tsd->cursors, "in check_handles");
 
@@ -170,46 +164,9 @@ static void check_handles(void *arg)
        }
 }
 
-static void dest_tsd(void *arg)
-{
-       if (arg != NULL) {
-               check_handles(arg);
-               free(arg);
-       }
-}
-
-/*
- * Ensure that we have a key for thread-specific data.  We don't
- * put anything in here that Citadel cares about; this is just database
- * related stuff like cursors and transactions.
- *
- * This should be called immediately after startup by any thread which wants
- * to use database calls, except for whatever thread calls open_databases.
- */
-void cdb_allocate_tsd(void)
-{
-       struct cdbtsd *tsd;
-
-       if (pthread_getspecific(tsdkey) != NULL)
-               return;
-
-       tsd = malloc(sizeof(struct cdbtsd));
-
-       tsd->tid = NULL;
-
-       memset(tsd->cursors, 0, sizeof tsd->cursors);
-       pthread_setspecific(tsdkey, tsd);
-}
-
-void cdb_free_tsd(void)
-{
-       dest_tsd(pthread_getspecific(tsdkey));
-       pthread_setspecific(tsdkey, NULL);
-}
-
 void cdb_check_handles(void)
 {
-       check_handles(pthread_getspecific(tsdkey));
+       check_handles(pthread_getspecific(ThreadKey));
 }
 
 
@@ -278,16 +235,20 @@ void cmd_cull(char *argbuf) {
 /*
  * Request a checkpoint of the database.
  */
-static void cdb_checkpoint(void)
+void cdb_checkpoint(void)
 {
        int ret;
-       static time_t last_run = 0L;
+//     static time_t last_run = 0L;
 
        /* Only do a checkpoint once per minute. */
+/*
+ * Don't need this any more, since the thread that calls us sleeps for 60 seconds between calls
        if ((time(NULL) - last_run) < 60L) {
                return;
        }
        last_run = time(NULL);
+*/
 
        lprintf(CTDL_DEBUG, "-- db checkpoint --\n");
        ret = dbenv->txn_checkpoint(dbenv,
@@ -307,29 +268,6 @@ static void cdb_checkpoint(void)
 }
 
 
-/*
- * Main loop for the checkpoint thread.
- */
-void *checkpoint_thread(void *arg) {
-       struct CitContext checkpointCC;
-
-       lprintf(CTDL_DEBUG, "checkpoint_thread() initializing\n");
-
-       memset(&checkpointCC, 0, sizeof(struct CitContext));
-       checkpointCC.internal_pgm = 1;
-       checkpointCC.cs_pid = 0;
-       pthread_setspecific(MyConKey, (void *)&checkpointCC );
-
-       cdb_allocate_tsd();
-
-       while (!time_to_die) {
-               cdb_checkpoint();
-               sleep(1);
-       }
-
-       lprintf(CTDL_DEBUG, "checkpoint_thread() exiting\n");
-       pthread_exit(NULL);
-}
 
 /*
  * Open the various databases we'll be using.  Any database which
@@ -397,27 +335,36 @@ void open_databases(void)
                exit(CTDLEXIT_DB);
        }
 
-       flags =
-           DB_CREATE | DB_RECOVER | DB_INIT_MPOOL |
-           DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD;
-       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n",
-               ctdl_data_dir, flags);
+       flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_RECOVER;
+       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", ctdl_data_dir, flags);
        ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       if (ret == DB_RUNRECOVERY) {
+               lprintf(CTDL_ALERT, "dbenv->open: %s\n", db_strerror(ret));
+               lprintf(CTDL_ALERT, "Attempting recovery...\n");
+               flags |= DB_RECOVER;
+               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       }
+       if (ret == DB_RUNRECOVERY) {
+               lprintf(CTDL_ALERT, "dbenv->open: %s\n", db_strerror(ret));
+               lprintf(CTDL_ALERT, "Attempting catastrophic recovery...\n");
+               flags &= ~DB_RECOVER;
+               flags |= DB_RECOVER_FATAL;
+               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       }
        if (ret) {
-               lprintf(CTDL_DEBUG, "cdb_*: dbenv->open: %s\n",
-                       db_strerror(ret));
+               lprintf(CTDL_DEBUG, "dbenv->open: %s\n", db_strerror(ret));
                dbenv->close(dbenv, 0);
                exit(CTDLEXIT_DB);
        }
 
-       lprintf(CTDL_INFO, "cdb_*: Starting up DB\n");
+       lprintf(CTDL_INFO, "Starting up DB\n");
 
        for (i = 0; i < MAXCDB; ++i) {
 
                /* Create a database handle */
                ret = db_create(&dbp[i], dbenv, 0);
                if (ret) {
-                       lprintf(CTDL_DEBUG, "cdb_*: db_create: %s\n",
+                       lprintf(CTDL_DEBUG, "db_create: %s\n",
                                db_strerror(ret));
                        exit(CTDLEXIT_DB);
                }
@@ -436,19 +383,12 @@ void open_databases(void)
                                   DB_CREATE | DB_AUTO_COMMIT | DB_THREAD,
                                   0600);
                if (ret) {
-                       lprintf(CTDL_EMERG, "cdb_*: db_open[%d]: %s\n", i,
+                       lprintf(CTDL_EMERG, "db_open[%d]: %s\n", i,
                                db_strerror(ret));
                        exit(CTDLEXIT_DB);
                }
        }
 
-       if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
-               lprintf(CTDL_EMERG, "cdb_*: pthread_key_create: %s\n",
-                       strerror(ret));
-               exit(CTDLEXIT_DB);
-       }
-
-       cdb_allocate_tsd();
 }
 
 
@@ -477,7 +417,7 @@ void cdb_chmod_data(void) {
                closedir(dp);
        }
 
-       lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
+       lprintf(CTDL_DEBUG, "open_databases() finished\n");
 
        CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
 }
@@ -492,23 +432,25 @@ void close_databases(void)
        int a;
        int ret;
 
-       cdb_free_tsd();
-
+       ctdl_thread_internal_free_tsd();
+       
        if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
                lprintf(CTDL_EMERG,
-                       "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
+                       "txn_checkpoint: %s\n", db_strerror(ret));
        }
 
        /* print some statistics... */
+#ifdef DB_STAT_ALL
        dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
+#endif
 
        /* close the tables */
        for (a = 0; a < MAXCDB; ++a) {
-               lprintf(CTDL_INFO, "cdb_*: Closing database %d\n", a);
+               lprintf(CTDL_INFO, "Closing database %d\n", a);
                ret = dbp[a]->close(dbp[a], 0);
                if (ret) {
                        lprintf(CTDL_EMERG,
-                               "cdb_*: db_close: %s\n", db_strerror(ret));
+                               "db_close: %s\n", db_strerror(ret));
                }
 
        }
@@ -517,7 +459,7 @@ void close_databases(void)
        ret = dbenv->close(dbenv, 0);
        if (ret) {
                lprintf(CTDL_EMERG,
-                       "cdb_*: DBENV->close: %s\n", db_strerror(ret));
+                       "DBENV->close: %s\n", db_strerror(ret));
        }
 }