arina-ielchiieva commented on a change in pull request #1345: DRILL-6494: Drill Plugins Handler URL: https://github.com/apache/drill/pull/1345#discussion_r198851192
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/store/StoragePluginsUpdater.java ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill.exec.store; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import com.jasonclawson.jackson.dataformat.hocon.HoconFactory; +import org.apache.drill.common.config.CommonConstants; +import org.apache.drill.common.config.LogicalPlanPersistence; +import org.apache.drill.common.exceptions.DrillIOException; +import org.apache.drill.common.logical.StoragePluginConfig; +import org.apache.drill.common.scanner.ClassPathScanner; +import org.apache.drill.exec.planner.logical.StoragePlugins; +import org.apache.drill.exec.server.DrillbitContext; +import org.apache.drill.exec.store.sys.PersistentStore; + +import javax.validation.constraints.NotNull; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * Drill plugins handler, which allows to update storage plugins configs from the + * {@link CommonConstants#STORAGE_PLUGINS_UPDATER_FILE} conf file + * + * TODO: it can be improved with configs versioning and service of creating {@link CommonConstants#STORAGE_PLUGINS_UPDATER_FILE} + */ +public class StoragePluginsUpdater implements StoragePluginsHandler { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StoragePluginsUpdater.class); + + final DrillbitContext context; + final LogicalPlanPersistence hoconLogicalPlanPersistence; + + public StoragePluginsUpdater(DrillbitContext context) { + this.context = context; + hoconLogicalPlanPersistence = new LogicalPlanPersistence(context.getConfig(), context.getClasspathScan(), + new ObjectMapper(new HoconFactory())); + } + + @Override + public StoragePlugins updatePlugins(StoragePlugins bootstrapPlugins, PersistentStore<StoragePluginConfig> persistentStore) { + StoragePlugins plugins = null; + Set<URL> urlSet = ClassPathScanner.forResource(CommonConstants.STORAGE_PLUGINS_UPDATER_FILE, false); + try { + if (!urlSet.isEmpty()) { + if (urlSet.size() != 1) { + throw new DrillIOException("More than one %s file is placed in Drill's classpath", + CommonConstants.STORAGE_PLUGINS_UPDATER_FILE); + } + URL fileUrl = urlSet.iterator().next(); + plugins = new StoragePlugins(new HashMap<>()); + if (bootstrapPlugins != null) { + plugins.putAll(bootstrapPlugins); + } + + String newPluginsData = Resources.toString(fileUrl, Charsets.UTF_8); + StoragePlugins newPlugins = hoconLogicalPlanPersistence.getMapper().readValue(newPluginsData, StoragePlugins.class); + + for (Map.Entry<String, StoragePluginConfig> newPlugin : newPlugins) { + String pluginName = newPlugin.getKey(); + StoragePluginConfig oldPluginConfig = persistentStore == null ? bootstrapPlugins.getConfig(pluginName) : + persistentStore.get(pluginName); + StoragePluginConfig updatedStatusPluginConfig = + determineUndefinedEnabledStatus(oldPluginConfig, newPlugin.getValue()); + plugins.put(pluginName, updatedStatusPluginConfig); + } + } + } catch (IOException e) { + logger.error("Failures are obtained while updating storage plugins from %s file. Proceed without updating", + CommonConstants.STORAGE_PLUGINS_UPDATER_FILE, e); + return bootstrapPlugins; + } + return plugins; + } + + /** + * Helper method to identify the enabled status for new storage plugins config. If this status is absent in the updater + * file, the status is kept from the configs, which are going to be updated + * + * @param oldPluginConfig current storage plugin config from persistence store or bootstrap config file + * @param newPluginConfig new storage plugin config + * @return new storage plugin config with updated enabled status + */ + private StoragePluginConfig determineUndefinedEnabledStatus(StoragePluginConfig oldPluginConfig, Review comment: Please rename: `updatePluginStatus`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services