Skip to content

Commit

Permalink
Retry db update if necessary.
Browse files Browse the repository at this point in the history
The first update fails often in Docker-Compose because the database is
not available yet.
  • Loading branch information
hylkevds committed Sep 6, 2018
1 parent c693cb3 commit 00be371
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 20 deletions.
Expand Up @@ -26,6 +26,9 @@
import de.fraunhofer.iosb.ilt.sta.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.sta.util.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.sta.util.NoSuchEntityException;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.Writer;

/**
*
Expand Down Expand Up @@ -122,7 +125,12 @@ public default void commitAndClose() {
/**
* Upgrade the storage backend.
*
* @return a log of what was done.
* @param out The Writer to append logging messages to.
* @return true if the upgrade was successful, false if upgrade should be
* tried again later.
* @throws de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException when
* upgrading fails and should not be attempted again at a later stage.
* @throws java.io.IOException when the Writer throws this exception.
*/
public String doUpgrades();
public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException;
}
Expand Up @@ -18,6 +18,9 @@

import de.fraunhofer.iosb.ilt.sta.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.sta.settings.PersistenceSettings;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.StringWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,12 +34,16 @@ public class PersistenceManagerFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(PersistenceManagerFactory.class);
private static PersistenceManagerFactory instance;
private static boolean maybeUpdateDatabase = true;

public static synchronized void init(CoreSettings settings) {
if (instance == null) {
instance = new PersistenceManagerFactory(settings);
maybeUpdateDatabase(settings, instance);
}
if (maybeUpdateDatabase) {
maybeUpdateDatabase(settings, instance);
}
}

public static PersistenceManagerFactory getInstance() {
Expand Down Expand Up @@ -79,8 +86,26 @@ public PersistenceManager create() {
private static void maybeUpdateDatabase(CoreSettings coreSettings, PersistenceManagerFactory instance) {
PersistenceSettings persistenceSettings = coreSettings.getPersistenceSettings();
if (persistenceSettings.isAutoUpdateDatabase()) {
String updateLog = instance.create().doUpgrades();
LOGGER.info("Database-update-log:\n{}", updateLog);
StringWriter updateLog = new StringWriter();
try {
boolean success = instance.create().doUpgrades(updateLog);
maybeUpdateDatabase = !success;
if (success) {
LOGGER.info("Database-update successful.");
} else {
LOGGER.info("Database-update not successful, trying again later.");
}
} catch (UpgradeFailedException ex) {
LOGGER.error("Database upgrade failed.", ex);
maybeUpdateDatabase = false;
} catch (IOException ex) {
// Should not happen, StringWriter does not throw IOExceptions.
LOGGER.error("Database upgrade failed.", ex);
}
String logString = updateLog.toString();
if (!logString.isEmpty()) {
LOGGER.info("Database-update-log:\n{}", logString);
}
}
}

Expand Down
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
* Karlsruhe, Germany.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.fraunhofer.iosb.ilt.sta.util;

/**
* The exception that should be thrown by a PersistenceManager when upgrading
* the database fails.
*
* @author Hylke van der Schaaf
*/
public class UpgradeFailedException extends Exception {

public UpgradeFailedException() {
}

public UpgradeFailedException(String message) {
super(message);
}

public UpgradeFailedException(Throwable cause) {
super(cause);
}

public UpgradeFailedException(String message, Throwable cause) {
super(message, cause);
}

}
Expand Up @@ -19,6 +19,7 @@

import de.fraunhofer.iosb.ilt.sta.persistence.PersistenceManagerFactory;
import de.fraunhofer.iosb.ilt.sta.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -97,8 +98,11 @@ protected void processPostRequest(HttpServletRequest request, HttpServletRespons
out.println("<h1>Servlet DatabaseStatus at " + request.getContextPath() + "</h1><p>Updating Database</p>");

out.println("<pre>");
String log = PersistenceManagerFactory.getInstance().create().doUpgrades();
out.println(log);
try {
PersistenceManagerFactory.getInstance().create().doUpgrades(out);
} catch (UpgradeFailedException ex) {
LOGGER.error("Could not initialise database.", ex);
}
out.println("</pre>");

out.println("<p>Done. <a href='DatabaseStatus'>Back...</a></p>");
Expand Down
Expand Up @@ -19,6 +19,7 @@

import de.fraunhofer.iosb.ilt.sta.persistence.PersistenceManagerFactory;
import de.fraunhofer.iosb.ilt.sta.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -97,8 +98,11 @@ protected void processPostRequest(HttpServletRequest request, HttpServletRespons
out.println("<h1>Servlet DatabaseStatus at " + request.getContextPath() + "</h1><p>Updating Database</p>");

out.println("<pre>");
String log = PersistenceManagerFactory.getInstance().create().doUpgrades();
out.println(log);
try {
PersistenceManagerFactory.getInstance().create().doUpgrades(out);
} catch (UpgradeFailedException ex) {
LOGGER.error("Could not initialise database.", ex);
}
out.println("</pre>");

out.println("<p>Done. <a href='DatabaseStatus'>Back...</a></p>");
Expand Down
Expand Up @@ -52,7 +52,10 @@
import de.fraunhofer.iosb.ilt.sta.settings.Settings;
import de.fraunhofer.iosb.ilt.sta.util.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.sta.util.NoSuchEntityException;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
Expand Down Expand Up @@ -513,8 +516,7 @@ public String checkForUpgrades() {
}

@Override
public String doUpgrades() {
StringWriter out = new StringWriter();
public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException {
try {
Connection connection = getConnection(settings);

Expand All @@ -530,13 +532,15 @@ public String doUpgrades() {
out.append("Failed to initialise database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
return false;

} catch (LiquibaseException ex) {
LOGGER.error("Could not upgrade database.", ex);
out.append("Failed to upgrade database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
throw new UpgradeFailedException(ex);
}
return out.toString();
return true;
}

}
Expand Up @@ -52,7 +52,10 @@
import de.fraunhofer.iosb.ilt.sta.settings.Settings;
import de.fraunhofer.iosb.ilt.sta.util.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.sta.util.NoSuchEntityException;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
Expand Down Expand Up @@ -513,8 +516,7 @@ public String checkForUpgrades() {
}

@Override
public String doUpgrades() {
StringWriter out = new StringWriter();
public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException {
try {
Connection connection = getConnection(settings);

Expand All @@ -530,13 +532,15 @@ public String doUpgrades() {
out.append("Failed to initialise database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
return false;

} catch (LiquibaseException ex) {
LOGGER.error("Could not upgrade database.", ex);
out.append("Failed to upgrade database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
throw new UpgradeFailedException(ex);
}
return out.toString();
return true;
}

}
Expand Up @@ -52,7 +52,10 @@
import de.fraunhofer.iosb.ilt.sta.settings.Settings;
import de.fraunhofer.iosb.ilt.sta.util.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.sta.util.NoSuchEntityException;
import de.fraunhofer.iosb.ilt.sta.util.UpgradeFailedException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
Expand Down Expand Up @@ -530,8 +533,7 @@ public String checkForUpgrades() {
}

@Override
public String doUpgrades() {
StringWriter out = new StringWriter();
public boolean doUpgrades(Writer out) throws UpgradeFailedException, IOException {
try {
Connection connection = getConnection(settings);

Expand All @@ -547,13 +549,15 @@ public String doUpgrades() {
out.append("Failed to initialise database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
return false;

} catch (LiquibaseException ex) {
LOGGER.error("Could not upgrade database.", ex);
out.append("Failed to upgrade database:\n");
out.append(ex.getLocalizedMessage());
out.append("\n");
throw new UpgradeFailedException(ex);
}
return out.toString();
return true;
}

}

0 comments on commit 00be371

Please sign in to comment.