Vojtech Szocs has uploaded a new change for review. Change subject: webadmin: Improve UI Plugin data reload logic ......................................................................
webadmin: Improve UI Plugin data reload logic UI Plugin infra was designed to allow reloading plugin meta-data and configuration without having to restart Engine, i.e. refreshing WebAdmin in browser reloads the plugin data automatically. This patch improves existing data reload logic, comparing cached data against (possibly missing) files, in addition to comparing (possibly added or modified) files against cached data. Change-Id: Ibefec7f4a5748b89e762b88eafed044fab977d04 Bug-Url: https://bugzilla.redhat.com/996563 Signed-off-by: Vojtech Szocs <[email protected]> --- M frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/plugin/PluginDataManager.java 1 file changed, 47 insertions(+), 9 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/22/20522/1 diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/plugin/PluginDataManager.java b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/plugin/PluginDataManager.java index b4f7d20..c1d37b2 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/plugin/PluginDataManager.java +++ b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/plugin/PluginDataManager.java @@ -6,13 +6,16 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import org.apache.log4j.Logger; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ObjectNode; import org.ovirt.engine.core.common.config.ConfigUtil; import org.ovirt.engine.core.utils.EngineLocalConfig; @@ -32,6 +35,8 @@ private static final String UI_PLUGIN_DIR = "ui-plugins"; //$NON-NLS-1$ private static final String JSON_FILE_SUFFIX = ".json"; //$NON-NLS-1$ private static final String CONFIG_FILE_SUFFIX = "-config" + JSON_FILE_SUFFIX; //$NON-NLS-1$ + + private static final long MISSING_FILE_LAST_MODIFIED = -1L; private static final Logger logger = Logger.getLogger(PluginDataManager.class); @@ -133,20 +138,34 @@ } void reloadData(File[] descriptorFiles, Map<String, PluginData> currentDataMapCopy) { + Map<String, PluginData> entriesToUpdate = new HashMap<String, PluginData>(); + Set<String> keysToRemove = new HashSet<String>(); + + // Optimization: make sure we don't check data that we already processed + Set<String> keysToCheckForRemoval = new HashSet<String>(currentDataMapCopy.keySet()); + + // Compare (possibly added or modified) files against cached data for (final File df : descriptorFiles) { final File cf = new File(pluginConfigDir, getConfigurationFileName(df)); - String descriptorFileName = df.getName(); + String descriptorFilePath = df.getAbsolutePath(); + PluginData currentData = currentDataMapCopy.get(descriptorFilePath); + long descriptorLastModified = df.lastModified(); - long configurationLastModified = isJsonFile(cf) ? cf.lastModified() : -1L; + long configurationLastModified = isJsonFile(cf) ? cf.lastModified() : MISSING_FILE_LAST_MODIFIED; // Check if data needs to be reloaded - PluginData currentData = currentDataMapCopy.get(descriptorFileName); boolean reloadDescriptor, reloadConfiguration; - if (currentDataMapCopy.containsKey(descriptorFileName)) { + if (currentDataMapCopy.containsKey(descriptorFilePath)) { reloadDescriptor = descriptorLastModified > currentData.getDescriptorLastModified(); - reloadConfiguration = configurationLastModified > currentData.getConfigurationLastModified() - || reloadDescriptor; + reloadConfiguration = configurationLastModified > currentData.getConfigurationLastModified(); + + // Change in descriptor causes reload of configuration + reloadConfiguration = reloadConfiguration || reloadDescriptor; + + // Refresh configuration if the corresponding file has gone missing + reloadConfiguration = reloadConfiguration || (configurationLastModified == MISSING_FILE_LAST_MODIFIED + && currentData.getConfigurationLastModified() != MISSING_FILE_LAST_MODIFIED); } else { reloadDescriptor = true; reloadConfiguration = true; @@ -173,7 +192,7 @@ configurationNode = readConfigurationNode(cf); if (configurationNode == null) { // Failed to read configuration data, use empty object - configurationNode = mapper.getNodeFactory().objectNode(); + configurationNode = createEmptyObjectNode(); } } else if (configurationNode == null) { logger.warn("UI plugin configuration node is null for [" + cf.getAbsolutePath() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ @@ -202,10 +221,25 @@ continue; } - // Update local data mapping - currentDataMapCopy.put(descriptorFileName, newData); + entriesToUpdate.put(descriptorFilePath, newData); + } + + keysToCheckForRemoval.remove(descriptorFilePath); + } + + // Compare cached data against (possibly missing) files + for (String descriptorFilePath : keysToCheckForRemoval) { + File df = new File(descriptorFilePath); + + if (!df.exists()) { + // Descriptor data file has gone missing + keysToRemove.add(descriptorFilePath); } } + + // Perform data updates + currentDataMapCopy.putAll(entriesToUpdate); + currentDataMapCopy.keySet().removeAll(keysToRemove); } boolean isJsonFile(File pathname) { @@ -230,4 +264,8 @@ return descriptorFile.getName().replace(JSON_FILE_SUFFIX, CONFIG_FILE_SUFFIX); } + ObjectNode createEmptyObjectNode() { + return mapper.getNodeFactory().objectNode(); + } + } -- To view, visit http://gerrit.ovirt.org/20522 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibefec7f4a5748b89e762b88eafed044fab977d04 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Vojtech Szocs <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
