Juan Hernandez has uploaded a new change for review. Change subject: core: Locate data source in a loop ......................................................................
core: Locate data source in a loop The deployment of the data source usually finishes before the application is started, but in some situations, for example with slow DNS, it can finish after the application has started, thus we can't assume that it has already been deployed. Change-Id: I72c99c61d05e8a1619c7d1fb70af956d1050eb3a Bug-Url: https://bugzilla.redhat.com/879904 Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacadeLocator.java 1 file changed, 49 insertions(+), 33 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/89/10189/1 diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacadeLocator.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacadeLocator.java index 40244c8..f085058 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacadeLocator.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacadeLocator.java @@ -25,15 +25,36 @@ private static DbFacade dbFacade; private static final Log log = LogFactory.getLog(DbFacadeLocator.class); + private static final String ENGINE_CONF_FILE = "/etc/ovirt-engine/engine.conf"; + private static final String ON_START_CONNECTION_TIMEOUT = "OnStartConnectionTimeout"; + private static final String CONNECTION_CHECK_INTERVAL = "ConnectionCheckInterval"; + private static final int DEFAULT_TIMEOUT_VALUE = 300000; + private static final int DEFAULT_INTERVAL_VALUE = 5000; + static { try { - // ok we need to locate the datasource - DataSource datasource = EjbUtils - .findResource(ContainerManagedResourceType.DATA_SOURCE); - if (datasource == null) - throw new RuntimeException("Datasource is not defined "); + // The data source may be deployed after this class is loaded, so we + // need to try to locate it in a loop: + DataSource datasource = null; + for(;;) { + // Try to locate the data source: + datasource = EjbUtils.findResource(ContainerManagedResourceType.DATA_SOURCE); + if (datasource != null) { + break; + } - // create the facade and return it + // Tell the user that the lookup failed but that we will try + // again in a few seconds: + log.warn( + "The datasource can't be located. This probably means " + + "that DNS is not working correctly or is slow, please " + + "check it. Will try again in a few seconds."); + + // Wait a bit before trying again: + Thread.sleep(DEFAULT_INTERVAL_VALUE); + } + + // Create the facade and return it: dbFacade = new DbFacade(); dbFacade.setDbEngineDialect(loadDbEngineDialect()); loadDbFacadeConfig(); @@ -68,56 +89,51 @@ } public static void loadDbFacadeConfig() throws Exception { - boolean configSucceeded = false; - final String ENGINE_CONF_FILE = "/etc/ovirt-engine/engine.conf"; - final String ON_START_CONNECTION_TIMEOUT = "OnStartConnectionTimeout"; - final String CONNECTION_CHECK_INTERVAL = "ConnectionCheckInterval"; - final String DEFAULT_TIMEOUT_VALUE = "300000"; - final String DEFAULT_INTERVAL_VALUE = "5000"; + // Start with the default values: + int onStartConnectionTimeout = DEFAULT_TIMEOUT_VALUE; + int connectionCheckInterval = DEFAULT_INTERVAL_VALUE; + + // Try to load the configuration: InputStream inputStream = null; try { - String onStartConnectionTimeout = null; - String connectionCheckInterval = null; + Properties props = new Properties(); if (FileUtil.fileExists(ENGINE_CONF_FILE)) { // File exists, load /etc/ovirt-engine/engine.conf and set values in DbFacade inputStream = new FileInputStream(ENGINE_CONF_FILE); props.load(inputStream); - onStartConnectionTimeout = props.getProperty(ON_START_CONNECTION_TIMEOUT); - connectionCheckInterval = props.getProperty(CONNECTION_CHECK_INTERVAL); - if (!validNumber(onStartConnectionTimeout)) { - onStartConnectionTimeout = DEFAULT_TIMEOUT_VALUE; + String value = null; + value = props.getProperty(ON_START_CONNECTION_TIMEOUT); + if (validNumber(value)) { + onStartConnectionTimeout = Integer.parseInt(value); } - if (!validNumber(connectionCheckInterval)) { - connectionCheckInterval = DEFAULT_INTERVAL_VALUE; + value = props.getProperty(CONNECTION_CHECK_INTERVAL); + if (validNumber(value)) { + connectionCheckInterval = Integer.parseInt(value); } - } else { + } + else { // File does not exist - use defaults log.warn(String.format("%1$s file is not found. Please check your engine installation. " + "Default values will be used" , ENGINE_CONF_FILE)); - onStartConnectionTimeout = DEFAULT_TIMEOUT_VALUE; - connectionCheckInterval = DEFAULT_INTERVAL_VALUE; } - dbFacade.setOnStartConnectionTimeout(Integer.parseInt(onStartConnectionTimeout)); - dbFacade.setConnectionCheckInterval(Integer.parseInt(connectionCheckInterval)); - configSucceeded = true; - } catch (Exception ex) { + } + catch (Exception ex) { log.error("Error in configuration of db facade " + ExceptionUtils.getMessage(ex)); - } finally { - if (!configSucceeded) { - dbFacade.setOnStartConnectionTimeout(300000); - dbFacade.setConnectionCheckInterval(1000); - } + } + finally { if (inputStream != null) { inputStream.close(); } } + // Copy the selected configuration to the facade: + dbFacade.setOnStartConnectionTimeout(onStartConnectionTimeout); + dbFacade.setConnectionCheckInterval(connectionCheckInterval); } private static boolean validNumber(String numberStr) { return numberStr != null && NumberUtils.isNumber(numberStr); } - } -- To view, visit http://gerrit.ovirt.org/10189 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I72c99c61d05e8a1619c7d1fb70af956d1050eb3a Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches