|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.sql.expressions; |
| 15 | + |
| 16 | +import com.facebook.presto.FullConnectorSession; |
| 17 | +import com.facebook.presto.Session; |
| 18 | +import com.facebook.presto.metadata.FunctionAndTypeManager; |
| 19 | +import com.facebook.presto.nodeManager.PluginNodeManager; |
| 20 | +import com.facebook.presto.spi.ConnectorSession; |
| 21 | +import com.facebook.presto.spi.NodeManager; |
| 22 | +import com.facebook.presto.spi.relation.ExpressionOptimizer; |
| 23 | +import com.facebook.presto.spi.sql.planner.ExpressionOptimizerContext; |
| 24 | +import com.facebook.presto.spi.sql.planner.ExpressionOptimizerFactory; |
| 25 | +import com.facebook.presto.sql.relational.FunctionResolution; |
| 26 | +import com.facebook.presto.sql.relational.RowExpressionOptimizer; |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | + |
| 29 | +import javax.inject.Inject; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.UncheckedIOException; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.ConcurrentHashMap; |
| 38 | + |
| 39 | +import static com.facebook.presto.SystemSessionProperties.getExpressionOptimizerName; |
| 40 | +import static com.facebook.presto.util.PropertiesUtil.loadProperties; |
| 41 | +import static com.google.common.base.Preconditions.checkArgument; |
| 42 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 43 | +import static com.google.common.io.Files.getNameWithoutExtension; |
| 44 | +import static java.util.Objects.requireNonNull; |
| 45 | + |
| 46 | +public class ExpressionOptimizerManager |
| 47 | +{ |
| 48 | + public static final String DEFAULT_EXPRESSION_OPTIMIZER_NAME = "default"; |
| 49 | + private static final File EXPRESSION_MANAGER_CONFIGURATION_DIRECTORY = new File("etc/expression-manager/"); |
| 50 | + private static final String EXPRESSION_MANAGER_FACTORY_NAME = "expression-manager-factory.name"; |
| 51 | + |
| 52 | + private final NodeManager nodeManager; |
| 53 | + private final FunctionAndTypeManager functionAndTypeManager; |
| 54 | + private final FunctionResolution functionResolution; |
| 55 | + private final File configurationDirectory; |
| 56 | + |
| 57 | + private final Map<String, ExpressionOptimizerFactory> expressionOptimizerFactories = new ConcurrentHashMap<>(); |
| 58 | + private final Map<String, ExpressionOptimizer> expressionOptimizers = new ConcurrentHashMap<>(); |
| 59 | + |
| 60 | + @Inject |
| 61 | + public ExpressionOptimizerManager(PluginNodeManager nodeManager, FunctionAndTypeManager functionAndTypeManager) |
| 62 | + { |
| 63 | + this(nodeManager, functionAndTypeManager, EXPRESSION_MANAGER_CONFIGURATION_DIRECTORY); |
| 64 | + } |
| 65 | + |
| 66 | + public ExpressionOptimizerManager(PluginNodeManager nodeManager, FunctionAndTypeManager functionAndTypeManager, File configurationDirectory) |
| 67 | + { |
| 68 | + requireNonNull(nodeManager, "nodeManager is null"); |
| 69 | + this.nodeManager = requireNonNull(nodeManager, "nodeManager is null"); |
| 70 | + this.functionAndTypeManager = requireNonNull(functionAndTypeManager, "functionAndTypeManager is null"); |
| 71 | + this.functionResolution = new FunctionResolution(functionAndTypeManager.getFunctionAndTypeResolver()); |
| 72 | + this.configurationDirectory = requireNonNull(configurationDirectory, "configurationDirectory is null"); |
| 73 | + expressionOptimizers.put(DEFAULT_EXPRESSION_OPTIMIZER_NAME, new RowExpressionOptimizer(functionAndTypeManager)); |
| 74 | + } |
| 75 | + |
| 76 | + public void loadExpressionOptimizerFactories() |
| 77 | + { |
| 78 | + try { |
| 79 | + for (File file : listFiles(configurationDirectory)) { |
| 80 | + if (file.isFile() && file.getName().endsWith(".properties")) { |
| 81 | + loadExpressionOptimizerFactory(file); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + catch (IOException e) { |
| 86 | + throw new UncheckedIOException("Failed to load expression manager configuration", e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void loadExpressionOptimizerFactory(File configurationFile) |
| 91 | + throws IOException |
| 92 | + { |
| 93 | + String name = getNameWithoutExtension(configurationFile.getName()); |
| 94 | + checkArgument(!isNullOrEmpty(name), "File name is empty, full path: %s", configurationFile.getAbsolutePath()); |
| 95 | + checkArgument(!name.equals(DEFAULT_EXPRESSION_OPTIMIZER_NAME), "Cannot name an expression optimizer instance %s", DEFAULT_EXPRESSION_OPTIMIZER_NAME); |
| 96 | + |
| 97 | + Map<String, String> properties = new HashMap<>(loadProperties(configurationFile)); |
| 98 | + String factoryName = properties.remove(EXPRESSION_MANAGER_FACTORY_NAME); |
| 99 | + checkArgument(!isNullOrEmpty(factoryName), "%s does not contain %s", configurationFile, EXPRESSION_MANAGER_FACTORY_NAME); |
| 100 | + checkArgument(expressionOptimizerFactories.containsKey(factoryName), |
| 101 | + "ExpressionOptimizerFactory %s is not registered, registered factories: ", factoryName, expressionOptimizerFactories.keySet()); |
| 102 | + |
| 103 | + ExpressionOptimizer optimizer = expressionOptimizerFactories.get(factoryName).createOptimizer( |
| 104 | + properties, |
| 105 | + new ExpressionOptimizerContext(nodeManager, functionAndTypeManager, functionResolution)); |
| 106 | + expressionOptimizers.put(name, optimizer); |
| 107 | + } |
| 108 | + |
| 109 | + public void addExpressionOptimizerFactory(ExpressionOptimizerFactory expressionOptimizerFactory) |
| 110 | + { |
| 111 | + String name = expressionOptimizerFactory.getName(); |
| 112 | + checkArgument( |
| 113 | + expressionOptimizerFactories.putIfAbsent(name, expressionOptimizerFactory) == null, |
| 114 | + "ExpressionOptimizerFactory %s is already registered", name); |
| 115 | + } |
| 116 | + |
| 117 | + public ExpressionOptimizer getExpressionOptimizer(ConnectorSession connectorSession) |
| 118 | + { |
| 119 | + // TODO: Remove this check once we have a more appropriate abstraction for session properties retrieved from plugins |
| 120 | + checkArgument(connectorSession instanceof FullConnectorSession, "connectorSession is not an instance of FullConnectorSession"); |
| 121 | + Session session = ((FullConnectorSession) connectorSession).getSession(); |
| 122 | + String expressionOptimizerName = getExpressionOptimizerName(session); |
| 123 | + checkArgument(expressionOptimizers.containsKey(expressionOptimizerName), "ExpressionOptimizer '%s' is not registered", expressionOptimizerName); |
| 124 | + return expressionOptimizers.get(expressionOptimizerName); |
| 125 | + } |
| 126 | + |
| 127 | + private static List<File> listFiles(File directory) |
| 128 | + { |
| 129 | + if (directory.isDirectory()) { |
| 130 | + File[] files = directory.listFiles(); |
| 131 | + if (files != null) { |
| 132 | + return ImmutableList.copyOf(files); |
| 133 | + } |
| 134 | + } |
| 135 | + return ImmutableList.of(); |
| 136 | + } |
| 137 | +} |
0 commit comments