]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
* Applied a patch sent in by Wilfried Goesgens which allows the various
[citadel.git] / citadel / database_sleepycat.c
index 1ce1e1b2b5cdee8ef917b2e7a9a0577d0921ad91..eb1e37f11e03d17ca00a7bb9e95183275a0ff958 100644 (file)
@@ -253,8 +253,15 @@ static void cdb_cull_logs(void)
                }
                free(list);
        }
+}
 
-       lprintf(CTDL_INFO, "Database log file cull ended.\n");
+/*
+ * Manually initiate log file cull.
+ */
+void cmd_cull(char *argbuf) {
+       if (CtdlAccessCheck(ac_internal)) return;
+       cdb_cull_logs();
+       cprintf("%d Database log file cull completed.\n", CIT_OK);
 }
 
 
@@ -264,8 +271,15 @@ static void cdb_cull_logs(void)
 static void cdb_checkpoint(void)
 {
        int ret;
-       /* static time_t last_cull = 0L; */
+       static time_t last_run = 0L;
+
+       /* Only do a checkpoint once per minute. */
+       if ((time(NULL) - last_run) < 60L) {
+               return;
+       }
+       last_run = time(NULL);
 
+       lprintf(CTDL_DEBUG, "-- db checkpoint --\n");
        ret = dbenv->txn_checkpoint(dbenv,
                                    MAX_CHECKPOINT_KBYTES,
                                    MAX_CHECKPOINT_MINUTES, 0);
@@ -277,14 +291,34 @@ static void cdb_checkpoint(void)
        }
 
        /* After a successful checkpoint, we can cull the unused logs */
-       cdb_cull_logs();
+       if (config.c_auto_cull) {
+               cdb_cull_logs();
+       }
+}
 
-       /* Cull the logs if we haven't done so for 24 hours
-          if ((time(NULL) - last_cull) > 86400L) {
-          last_cull = time(NULL);
-          cdb_cull_logs();
-          } */
 
+/*
+ * 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);
 }
 
 /*
@@ -304,9 +338,12 @@ void open_databases(void)
        struct dirent *d;
        char filename[PATH_MAX];
 
-
+#ifndef HAVE_DATA_DIR
        getcwd(dbdirname, sizeof dbdirname);
        strcat(dbdirname, "/data");
+#else
+       strcat(dbdirname, DATA_DIR"/data");
+#endif
 
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() starting\n");
        lprintf(CTDL_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
@@ -406,7 +443,6 @@ void open_databases(void)
        }
 
        cdb_allocate_tsd();
-       CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
 
        /* Now make sure we own all the files, because in a few milliseconds
         * we're going to drop root privs.
@@ -425,6 +461,8 @@ void open_databases(void)
        }
 
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
+
+       CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
 }
 
 
@@ -873,7 +911,7 @@ void cdb_end_transaction(void)
  */
 void cdb_trunc(int cdb)
 {
-       //DB_TXN *tid;
+       /* DB_TXN *tid; */
        int ret;
        u_int32_t count;
 
@@ -886,14 +924,14 @@ void cdb_trunc(int cdb)
                             "attempt to write during r/o cursor");
 
              retry:
-               //txbegin(&tid);
+               /* txbegin(&tid); */
 
                if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
                                              NULL,     /* transaction ID */
                                              &count,   /* #rows deleted */
                                              0))) {    /* flags */
                        if (ret == DB_LOCK_DEADLOCK) {
-                               //txabort(tid);
+                               /* txabort(tid); */
                                goto retry;
                        } else {
                                lprintf(CTDL_EMERG,
@@ -902,7 +940,7 @@ void cdb_trunc(int cdb)
                                abort();
                        }
                } else {
-                       //txcommit(tid);
+                       /* txcommit(tid); */
                }
        }
 }