View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.proxy.backend.config;
19  
20  import com.google.common.base.Preconditions;
21  import lombok.AccessLevel;
22  import lombok.NoArgsConstructor;
23  import lombok.SneakyThrows;
24  import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
25  import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
26  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
27  import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
28  import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
29  import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
30  import org.apache.shardingsphere.proxy.backend.config.checker.YamlProxyConfigurationChecker;
31  import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
32  import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyServerConfiguration;
33  
34  import java.io.File;
35  import java.io.IOException;
36  import java.net.URISyntaxException;
37  import java.net.URL;
38  import java.util.Collection;
39  import java.util.HashSet;
40  import java.util.LinkedHashMap;
41  import java.util.LinkedList;
42  import java.util.Map;
43  import java.util.Map.Entry;
44  import java.util.Optional;
45  import java.util.regex.Pattern;
46  import java.util.stream.Collectors;
47  
48  /**
49   * Proxy configuration loader.
50   */
51  @NoArgsConstructor(access = AccessLevel.PRIVATE)
52  public final class ProxyConfigurationLoader {
53      
54      private static final String GLOBAL_CONFIG_FILE = "global.yaml";
55      
56      private static final Pattern DATABASE_CONFIG_FILE_PATTERN = Pattern.compile("database-.+\\.yaml");
57      
58      // TODO remove COMPATIBLE_GLOBAL_CONFIG_FILE in next major version
59      /**
60       * to be removed.
61       * 
62       * @deprecated to be removed
63       */
64      @Deprecated
65      private static final String COMPATIBLE_GLOBAL_CONFIG_FILE = "server.yaml";
66      
67      // TODO remove COMPATIBLE_DATABASE_CONFIG_FILE_PATTERN in next major version
68      /**
69       * to be removed.
70       *
71       * @deprecated to be removed
72       */
73      @Deprecated
74      private static final Pattern COMPATIBLE_DATABASE_CONFIG_FILE_PATTERN = Pattern.compile("config-.+\\.yaml");
75      
76      /**
77       * Load configuration of ShardingSphere-Proxy.
78       *
79       * @param path configuration path of ShardingSphere-Proxy
80       * @return configuration of ShardingSphere-Proxy
81       * @throws IOException IO exception
82       */
83      public static YamlProxyConfiguration load(final String path) throws IOException {
84          YamlProxyServerConfiguration serverConfig = loadServerConfiguration(getGlobalConfigFile(path));
85          File configPath = getResourceFile(path);
86          Collection<YamlProxyDatabaseConfiguration> databaseConfigs = loadDatabaseConfigurations(configPath);
87          YamlProxyConfigurationChecker.checkDataSources(serverConfig.getDataSources(), databaseConfigs);
88          return new YamlProxyConfiguration(serverConfig, databaseConfigs.stream().collect(Collectors.toMap(
89                  YamlProxyDatabaseConfiguration::getDatabaseName, each -> each, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)));
90      }
91      
92      private static File getGlobalConfigFile(final String path) {
93          File result = getResourceFile(String.join("/", path, GLOBAL_CONFIG_FILE));
94          return result.exists() ? result : getResourceFile(String.join("/", path, COMPATIBLE_GLOBAL_CONFIG_FILE));
95      }
96      
97      @SneakyThrows(URISyntaxException.class)
98      private static File getResourceFile(final String path) {
99          URL url = ProxyConfigurationLoader.class.getResource(path);
100         return null == url ? new File(path) : new File(url.toURI().getPath());
101     }
102     
103     private static YamlProxyServerConfiguration loadServerConfiguration(final File yamlFile) throws IOException {
104         YamlProxyServerConfiguration result = YamlEngine.unmarshal(yamlFile, YamlProxyServerConfiguration.class);
105         return rebuildGlobalRuleConfiguration(result);
106     }
107     
108     private static YamlProxyServerConfiguration rebuildGlobalRuleConfiguration(final YamlProxyServerConfiguration serverConfig) {
109         serverConfig.getRules().removeIf(YamlGlobalRuleConfiguration.class::isInstance);
110         if (null != serverConfig.getAuthority()) {
111             serverConfig.getRules().add(serverConfig.getAuthority());
112         }
113         if (null != serverConfig.getTransaction()) {
114             serverConfig.getRules().add(serverConfig.getTransaction());
115         }
116         if (null != serverConfig.getGlobalClock()) {
117             serverConfig.getRules().add(serverConfig.getGlobalClock());
118         }
119         if (null != serverConfig.getSqlParser()) {
120             serverConfig.getRules().add(serverConfig.getSqlParser());
121         }
122         if (null != serverConfig.getSqlTranslator()) {
123             serverConfig.getRules().add(serverConfig.getSqlTranslator());
124         }
125         if (null != serverConfig.getTraffic()) {
126             serverConfig.getRules().add(serverConfig.getTraffic());
127         }
128         if (null != serverConfig.getLogging()) {
129             serverConfig.getRules().add(serverConfig.getLogging());
130         }
131         if (null != serverConfig.getSqlFederation()) {
132             serverConfig.getRules().add(serverConfig.getSqlFederation());
133         }
134         return serverConfig;
135     }
136     
137     private static Collection<YamlProxyDatabaseConfiguration> loadDatabaseConfigurations(final File configPath) throws IOException {
138         Collection<String> loadedDatabaseNames = new HashSet<>();
139         Collection<YamlProxyDatabaseConfiguration> result = new LinkedList<>();
140         for (File each : findRuleConfigurationFiles(configPath)) {
141             loadDatabaseConfiguration(each).ifPresent(optional -> {
142                 Preconditions.checkState(loadedDatabaseNames.add(optional.getDatabaseName()), "Database name `%s` must unique at all database configurations.", optional.getDatabaseName());
143                 result.add(optional);
144             });
145         }
146         return result;
147     }
148     
149     private static Optional<YamlProxyDatabaseConfiguration> loadDatabaseConfiguration(final File yamlFile) throws IOException {
150         YamlProxyDatabaseConfiguration result = YamlEngine.unmarshal(yamlFile, YamlProxyDatabaseConfiguration.class);
151         if (result.isEmpty()) {
152             return Optional.empty();
153         }
154         Preconditions.checkNotNull(result.getDatabaseName(), "Property `databaseName` in file `%s` is required.", yamlFile.getName());
155         checkDuplicateRule(result.getRules(), yamlFile);
156         return Optional.of(result);
157     }
158     
159     private static void checkDuplicateRule(final Collection<YamlRuleConfiguration> ruleConfigs, final File yamlFile) {
160         if (ruleConfigs.isEmpty()) {
161             return;
162         }
163         Map<Class<? extends RuleConfiguration>, Long> ruleConfigTypeCountMap = ruleConfigs.stream()
164                 .collect(Collectors.groupingBy(YamlRuleConfiguration::getRuleConfigurationType, Collectors.counting()));
165         Optional<Entry<Class<? extends RuleConfiguration>, Long>> duplicateRuleConfig = ruleConfigTypeCountMap.entrySet().stream().filter(each -> each.getValue() > 1).findFirst();
166         if (duplicateRuleConfig.isPresent()) {
167             throw new IllegalStateException(String.format("Duplicate rule tag `!%s` in file `%s`", getDuplicateRuleTagName(duplicateRuleConfig.get().getKey()), yamlFile.getName()));
168         }
169     }
170     
171     @SuppressWarnings("rawtypes")
172     private static Object getDuplicateRuleTagName(final Class<? extends RuleConfiguration> ruleConfigClass) {
173         Optional<YamlRuleConfigurationSwapper> result = ShardingSphereServiceLoader.getServiceInstances(YamlRuleConfigurationSwapper.class)
174                 .stream().filter(each -> ruleConfigClass.equals(each.getTypeClass())).findFirst();
175         return result.orElseThrow(() -> new IllegalStateException("Not find rule tag name of class " + ruleConfigClass));
176     }
177     
178     private static File[] findRuleConfigurationFiles(final File path) {
179         return path.listFiles(each -> DATABASE_CONFIG_FILE_PATTERN.matcher(each.getName()).matches() || COMPATIBLE_DATABASE_CONFIG_FILE_PATTERN.matcher(each.getName()).matches());
180     }
181 }