Changeset: 3d4378764615 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3d4378764615
Modified Files:
        gdk/gdk_logger.c
        gdk/gdk_logger_internals.h
Branch: multi-cachelock
Log Message:

allow log rotation to a new file concurrently with the flush of the previous 
log file


diffs (92 lines):

diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1094,6 +1094,9 @@ log_close_input(logger *lg)
 static inline void
 log_close_output(logger *lg)
 {
+       if (lg->flushing_output_log)
+               return;
+
        if (!LOG_DISABLED(lg))
                close_stream(lg->output_log);
        lg->output_log = NULL;
@@ -2781,10 +2784,21 @@ log_delta(logger *lg, BAT *uid, BAT *uva
 #define LOG_LARGE      (LL_CONSTANT(2)*1024*1024*1024)
 
 static gdk_return
-new_logfile(logger *lg)
+new_logfile(logger *lg, stream* output_log, ulng id)
 {
        assert(!LOG_DISABLED(lg));
 
+       MT_lock_set(&lg->rotation_lock);
+       assert(lg->flushing_output_log);
+       lg->flushing_output_log = false;
+       if (lg->id != id) {
+               /* lg->output_log was rotated during the flush */
+               assert(lg->output_log != output_log && lg->id > id);
+               close_stream(output_log);
+               MT_lock_unset(&lg->rotation_lock);
+               return GDK_SUCCEED;
+       }
+       MT_lock_unset(&lg->rotation_lock);
 
        const lng log_large = (GDKdebug & FORCEMITOMASK)?LOG_MINI:LOG_LARGE;
 
@@ -2907,7 +2921,12 @@ log_tflush(logger* lg, ulng log_file_id,
                return GDK_SUCCEED;
        }
 
-       if (log_file_id == lg->id) {
+
+       ulng id;
+       MT_lock_set(&lg->rotation_lock);
+       id = lg->id;
+       MT_lock_unset(&lg->rotation_lock);
+       if (log_file_id == id) {
                unsigned int number = request_number_flush_queue(lg);
 
                MT_lock_set(&lg->flush_lock);
@@ -2916,10 +2935,18 @@ log_tflush(logger* lg, ulng log_file_id,
                        /* number of transactions in the group commit */
                        const int fqueue_length = flush_queue_length(lg);
                        /* flush + fsync */
-                       if (mnstr_flush(lg->output_log, MNSTR_FLUSH_DATA) ||
-                                       (!(GDKdebug & NOSYNCMASK) && 
mnstr_fsync(lg->output_log)) ||
-                                       new_logfile(lg) != GDK_SUCCEED) {
+                       MT_lock_set(&lg->rotation_lock);
+                       lg->flushing_output_log = true;
+                       stream* output_log = lg->output_log;
+                       id = lg->id;
+                       MT_lock_unset(&lg->rotation_lock);
+                       if (mnstr_flush(output_log, MNSTR_FLUSH_DATA) ||
+                                       (!(GDKdebug & NOSYNCMASK) && 
mnstr_fsync(output_log)) ||
+                                       new_logfile(lg, output_log, id) != 
GDK_SUCCEED) {
                                /* flush failed */
+                               MT_lock_set(&lg->rotation_lock);
+                               lg->flushing_output_log = false;
+                               MT_lock_unset(&lg->rotation_lock);
                                MT_lock_unset(&lg->flush_lock);
                                (void) ATOMIC_DEC(&lg->refcount);
                                return GDK_FAIL;
@@ -3114,7 +3141,7 @@ log_tstart(logger *lg, bool flushnow, ul
 {
        MT_lock_set(&lg->rotation_lock);
        log_lock(lg);
-       if (flushnow || (lg->request_rotation && ATOMIC_GET(&lg->refcount) == 
0)) {
+       if ((flushnow || (lg->request_rotation && ATOMIC_GET(&lg->refcount) == 
0)) && lg->end > 0) {
                lg->id++;
                log_close_output(lg);
                /* start new file */
diff --git a/gdk/gdk_logger_internals.h b/gdk/gdk_logger_internals.h
--- a/gdk/gdk_logger_internals.h
+++ b/gdk/gdk_logger_internals.h
@@ -45,6 +45,7 @@ struct logger {
        lng end;                /* end of pre-allocated blocks for faster 
f(data)sync */
 
        ATOMIC_TYPE refcount; /* Number of active writers and flushers in the 
logger */ // TODO check refcount in c->log and c->end
+       bool flushing_output_log; /* prevent output_log that is currently being 
flushed from being closed */
        MT_Lock rotation_lock;
        MT_Lock lock;
        /* Store log_bids (int) to circumvent trouble with reference counting */
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to