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.getSqlFederation()) {
126             serverConfig.getRules().add(serverConfig.getSqlFederation());
127         }
128         return serverConfig;
129     }
130     
131     private static Collection<YamlProxyDatabaseConfiguration> loadDatabaseConfigurations(final File configPath) throws IOException {
132         File[] ruleConfigFiles = findRuleConfigurationFiles(configPath);
133         Collection<String> loadedDatabaseNames = new HashSet<>(ruleConfigFiles.length);
134         Collection<YamlProxyDatabaseConfiguration> result = new LinkedList<>();
135         for (File each : ruleConfigFiles) {
136             loadDatabaseConfiguration(each).ifPresent(optional -> {
137                 Preconditions.checkState(loadedDatabaseNames.add(optional.getDatabaseName()), "Database name `%s` must unique at all database configurations.", optional.getDatabaseName());
138                 result.add(optional);
139             });
140         }
141         return result;
142     }
143     
144     private static Optional<YamlProxyDatabaseConfiguration> loadDatabaseConfiguration(final File yamlFile) throws IOException {
145         YamlProxyDatabaseConfiguration result = YamlEngine.unmarshal(yamlFile, YamlProxyDatabaseConfiguration.class);
146         if (result.isEmpty()) {
147             return Optional.empty();
148         }
149         Preconditions.checkNotNull(result.getDatabaseName(), "Property `databaseName` in file `%s` is required.", yamlFile.getName());
150         checkDuplicateRule(result.getRules(), yamlFile);
151         return Optional.of(result);
152     }
153     
154     private static void checkDuplicateRule(final Collection<YamlRuleConfiguration> ruleConfigs, final File yamlFile) {
155         if (ruleConfigs.isEmpty()) {
156             return;
157         }
158         Map<Class<? extends RuleConfiguration>, Long> ruleConfigTypeCountMap = ruleConfigs.stream()
159                 .collect(Collectors.groupingBy(YamlRuleConfiguration::getRuleConfigurationType, Collectors.counting()));
160         Optional<Entry<Class<? extends RuleConfiguration>, Long>> duplicateRuleConfig = ruleConfigTypeCountMap.entrySet().stream().filter(each -> each.getValue() > 1L).findFirst();
161         if (duplicateRuleConfig.isPresent()) {
162             throw new IllegalStateException(String.format("Duplicate rule tag `!%s` in file `%s`", getDuplicateRuleTagName(duplicateRuleConfig.get().getKey()), yamlFile.getName()));
163         }
164     }
165     
166     @SuppressWarnings("rawtypes")
167     private static Object getDuplicateRuleTagName(final Class<? extends RuleConfiguration> ruleConfigClass) {
168         Optional<YamlRuleConfigurationSwapper> result = ShardingSphereServiceLoader.getServiceInstances(YamlRuleConfigurationSwapper.class)
169                 .stream().filter(each -> ruleConfigClass.equals(each.getTypeClass())).findFirst();
170         return result.orElseThrow(() -> new IllegalStateException("Not find rule tag name of class " + ruleConfigClass));
171     }
172     
173     private static File[] findRuleConfigurationFiles(final File path) {
174         return path.listFiles(each -> DATABASE_CONFIG_FILE_PATTERN.matcher(each.getName()).matches() || COMPATIBLE_DATABASE_CONFIG_FILE_PATTERN.matcher(each.getName()).matches());
175     }
176 }