Repository: ambari Updated Branches: refs/heads/trunk 813841f87 -> 309dbd758
AMBARI-20096. Log Search: only update schema file for Solr if it has any new fields in the local file (oleewere) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/309dbd75 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/309dbd75 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/309dbd75 Branch: refs/heads/trunk Commit: 309dbd7581bb7826f80a21e046721ee30acc27fb Parents: 813841f Author: oleewere <[email protected]> Authored: Mon Feb 20 15:17:59 2017 +0100 Committer: oleewere <[email protected]> Committed: Wed Feb 22 12:45:56 2017 +0100 ---------------------------------------------------------------------- .../handler/UploadConfigurationHandler.java | 65 +++++++++++++++++--- 1 file changed, 58 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/309dbd75/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/handler/UploadConfigurationHandler.java ---------------------------------------------------------------------- diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/handler/UploadConfigurationHandler.java b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/handler/UploadConfigurationHandler.java index 23defea..27a6705 100644 --- a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/handler/UploadConfigurationHandler.java +++ b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/handler/UploadConfigurationHandler.java @@ -19,6 +19,7 @@ package org.apache.ambari.logsearch.handler; import org.apache.ambari.logsearch.conf.SolrPropsConfig; +import org.apache.commons.configuration.XMLConfiguration; import org.apache.commons.io.FileUtils; import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.apache.solr.common.cloud.SolrZkClient; @@ -29,6 +30,8 @@ import java.io.File; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; public class UploadConfigurationHandler implements SolrZkRequestHandler<Boolean> { @@ -37,6 +40,9 @@ public class UploadConfigurationHandler implements SolrZkRequestHandler<Boolean> private static final String SCHEMA_FILE = "managed-schema"; private static final String SOLR_CONFIG_FILE = "solrconfig.xml"; + private static final String FIELD_NAME_PATH = "field[@name]"; + private static final String FIELD_TYPE_NAME_PATH = "fieldType[@name]"; + private static final String DYNAMIC_FIELD_NAME_PATH = "dynamicField[@name]"; private File configSetFolder; @@ -65,13 +71,17 @@ public class UploadConfigurationHandler implements SolrZkRequestHandler<Boolean> File[] listOfFiles = configSetFolder.listFiles(); if (listOfFiles != null) { for (File file : listOfFiles) { - if (file.getName().equals(SOLR_CONFIG_FILE) || file.getName().equals(SCHEMA_FILE)) { // TODO: try to find an another solution to reload schema - if (!FileUtils.contentEquals(file, new File(String.format("%s%s%s", downloadFolderLocation, separator, file.getName())))){ - LOG.info("One of the local solr config file differs ('{}'), upload config set to zookeeper", file.getName()); - zkConfigManager.uploadConfigDir(configSetFolder.toPath(), solrPropsConfig.getConfigName()); - reloadCollectionNeeded = true; - break; - } + if (file.getName().equals(SOLR_CONFIG_FILE) && !FileUtils.contentEquals(file, new File(String.format("%s%s%s", downloadFolderLocation, separator, file.getName())))) { + LOG.info("Solr config file differs ('{}'), upload config set to zookeeper", file.getName()); + zkConfigManager.uploadConfigDir(configSetFolder.toPath(), solrPropsConfig.getConfigName()); + reloadCollectionNeeded = true; + break; + } + if (file.getName().equals(SCHEMA_FILE) && localSchemaFileHasMoreFields(file, new File(String.format("%s%s%s", downloadFolderLocation, separator, file.getName())))) { + LOG.info("Solr schema file differs ('{}'), upload config set to zookeeper", file.getName()); + zkConfigManager.uploadConfigDir(configSetFolder.toPath(), solrPropsConfig.getConfigName()); + reloadCollectionNeeded = true; + break; } } } @@ -97,4 +107,45 @@ public class UploadConfigurationHandler implements SolrZkRequestHandler<Boolean> return reloadCollectionNeeded; } + private boolean localSchemaFileHasMoreFields(File localFile, File downloadedFile) { + try { + XMLConfiguration localFileXml = new XMLConfiguration(localFile); + XMLConfiguration downloadedFileXml = new XMLConfiguration(downloadedFile); + + List<String> localFieldNames = (ArrayList<String>) localFileXml.getProperty(FIELD_NAME_PATH); + List<String> localFieldTypes = (ArrayList<String>) localFileXml.getProperty(FIELD_TYPE_NAME_PATH); + List<String> localDynamicFields = (ArrayList<String>) localFileXml.getProperty(DYNAMIC_FIELD_NAME_PATH); + + List<String> fieldNames = (ArrayList<String>) downloadedFileXml.getProperty(FIELD_NAME_PATH); + List<String> fieldTypes = (ArrayList<String>) downloadedFileXml.getProperty(FIELD_TYPE_NAME_PATH); + List<String> dynamicFields = (ArrayList<String>) downloadedFileXml.getProperty(DYNAMIC_FIELD_NAME_PATH); + + boolean fieldNameHasDiff = hasMoreFields(localFieldNames, fieldNames, FIELD_NAME_PATH); + boolean fieldTypeHasDiff = hasMoreFields(localFieldTypes, fieldTypes, FIELD_TYPE_NAME_PATH); + boolean dynamicFieldNameHasDiff = hasMoreFields(localDynamicFields, dynamicFields, DYNAMIC_FIELD_NAME_PATH); + + return fieldNameHasDiff || fieldTypeHasDiff || dynamicFieldNameHasDiff; + } catch (Exception e) { + throw new RuntimeException("Exception during schema xml parsing.", e); + } + } + + private boolean hasMoreFields(List<String> localFields, List<String> fields, String tag) { + boolean result = false; + if (localFields != null) { + if (fields == null) { + result = true; + } else { + localFields.removeAll(fields); + if (!localFields.isEmpty()) { + result = true; + } + } + } + if (result) { + LOG.info("Found new fields or field types in local schema file.: {} ({})", localFields.toString(), tag); + } + return result; + } + }
