Changeset: 347320055651 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/347320055651
Modified Files:
        gdk/gdk_bbp.c
Branch: Jul2021
Log Message:

Cleanup bat directory after renaming string offset heaps; improve said renaming.


diffs (136 lines):

diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -1027,16 +1027,40 @@ movestrbats(void)
                        /* not a valid BAT */
                        continue;
                }
-               if (b->ttype != TYPE_str || b->twidth == SIZEOF_VAR_T)
+               if (b->ttype != TYPE_str || b->twidth == SIZEOF_VAR_T || 
b->batCount == 0)
                        continue;
                char *oldpath = GDKfilepath(0, BATDIR, 
BBP_physical(b->batCacheid), "tail");
                char *newpath = GDKfilepath(0, BATDIR, b->theap->filename, 
NULL);
                int ret = -1;
-               if (oldpath != NULL && newpath != NULL)
-                       ret = MT_rename(oldpath, newpath);
+               if (oldpath != NULL && newpath != NULL) {
+                       struct stat oldst, newst;
+                       bool oldexist = MT_stat(oldpath, &oldst) == 0;
+                       bool newexist = MT_stat(newpath, &newst) == 0;
+                       if (newexist) {
+                               if (oldexist) {
+                                       if (oldst.st_mtime > newst.st_mtime) {
+                                               GDKerror("both %s and %s exist 
with %s unexpectedly newer: manual intervention required\n", oldpath, newpath, 
oldpath);
+                                               ret = -1;
+                                       } else {
+                                               TRC_WARNING(GDK, "both %s and 
%s exist, removing %s\n", oldpath, newpath, oldpath);
+                                               ret = MT_remove(oldpath);
+                                       }
+                               } else {
+                                       /* already good */
+                                       ret = 0;
+                               }
+                       } else if (oldexist) {
+                               TRC_DEBUG(IO_, "rename %s to %s\n", oldpath, 
newpath);
+                               ret = MT_rename(oldpath, newpath);
+                       } else {
+                               /* neither file exists: may be ok, but
+                                * will be checked later */
+                               ret = 0;
+                       }
+               }
                GDKfree(oldpath);
                GDKfree(newpath);
-               if (ret < 0)
+               if (ret == -1)
                        return GDK_FAIL;
        }
        return GDK_SUCCEED;
@@ -1257,6 +1281,62 @@ BBPinit(void)
        if (BBPcheckbats(bbpversion) != GDK_SUCCEED)
                return GDK_FAIL;
 
+#ifdef GDKLIBRARY_TAILN
+       char *needstrbatmove;
+       needstrbatmove = GDKfilepath(0, BATDIR, "needstrbatmove", NULL);
+       if (bbpversion <= GDKLIBRARY_TAILN) {
+               /* create signal file that we need to rename string
+                * offset heaps */
+               int fd = MT_open(needstrbatmove, O_WRONLY | O_CREAT);
+               if (fd < 0) {
+                       TRC_CRITICAL(GDK, "cannot create signal file 
needstrbatmove.\n");
+                       GDKfree(needstrbatmove);
+                       return GDK_FAIL;
+               }
+               close(fd);
+       } else {
+               /* check signal file whether we need to rename string
+                * offset heaps */
+               int fd = MT_open(needstrbatmove, O_RDONLY);
+               if (fd >= 0) {
+                       /* yes, we do */
+                       close(fd);
+               } else if (errno == ENOENT) {
+                       /* no, we don't: set var to NULL */
+                       GDKfree(needstrbatmove);
+                       needstrbatmove = NULL;
+               } else {
+                       GDKsyserror("unexpected error opening %s\n", 
needstrbatmove);
+                       GDKfree(needstrbatmove);
+                       return GDK_FAIL;
+               }
+       }
+#endif
+
+       if (bbpversion < GDKLIBRARY && TMcommit() != GDK_SUCCEED) {
+               TRC_CRITICAL(GDK, "TMcommit failed\n");
+               return GDK_FAIL;
+       }
+
+#ifdef GDKLIBRARY_TAILN
+       /* we rename the offset heaps after the above commit: in this
+        * version we accept both the old and new names, but we want to
+        * convert so that future versions only have the new name */
+       if (needstrbatmove) {
+               /* note, if renaming fails, nothing is lost: a next
+                * invocation will just try again; an older version of
+                * mserver will not work because of the TMcommit
+                * above */
+               if (movestrbats() != GDK_SUCCEED) {
+                       GDKfree(needstrbatmove);
+                       return GDK_FAIL;
+               }
+               MT_remove(needstrbatmove);
+               GDKfree(needstrbatmove);
+               needstrbatmove = NULL;
+       }
+#endif
+
        /* cleanup any leftovers (must be done after BBPrecover) */
        for (i = 0; i < MAXFARMS && BBPfarms[i].dirname != NULL; i++) {
                int j;
@@ -1277,24 +1357,6 @@ BBPinit(void)
                }
        }
 
-       if (bbpversion < GDKLIBRARY && TMcommit() != GDK_SUCCEED) {
-               TRC_CRITICAL(GDK, "TMcommit failed\n");
-               return GDK_FAIL;
-       }
-#ifdef GDKLIBRARY_TAILN
-       /* we rename the offset heaps after the above commit: in this
-        * version we accept both the old and new names, but we want to
-        * convert so that future versions only have the new name */
-       if (bbpversion <= GDKLIBRARY_TAILN) {
-               /* note, if renaming fails, nothing is lost: a next
-                * invocation will just try again; an older version of
-                * mserver will not work because of the TMcommit
-                * above */
-               if (movestrbats() != GDK_SUCCEED)
-                       return GDK_FAIL;
-       }
-#endif
-
        manager = THRcreate(BBPmanager, NULL, MT_THR_DETACHED, "BBPmanager");
        return GDK_SUCCEED;
 
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to