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.infra.metadata.database;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
23  import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
24  import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
25  import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
26  import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
27  import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
28  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
29  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
30  import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
31  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
32  import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
33  import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
34  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
35  
36  import javax.sql.DataSource;
37  import java.util.Collection;
38  import java.util.LinkedHashMap;
39  import java.util.LinkedList;
40  import java.util.Map;
41  import java.util.Map.Entry;
42  import java.util.Optional;
43  import java.util.concurrent.ConcurrentHashMap;
44  import java.util.stream.Collectors;
45  
46  /**
47   * ShardingSphere database.
48   */
49  @Getter
50  public final class ShardingSphereDatabase {
51      
52      private final String name;
53      
54      private final DatabaseType protocolType;
55      
56      private final ResourceMetaData resourceMetaData;
57      
58      private final RuleMetaData ruleMetaData;
59      
60      @Getter(AccessLevel.NONE)
61      private final Map<ShardingSphereIdentifier, ShardingSphereSchema> schemas;
62      
63      public ShardingSphereDatabase(final String name, final DatabaseType protocolType, final ResourceMetaData resourceMetaData,
64                                    final RuleMetaData ruleMetaData, final Collection<ShardingSphereSchema> schemas) {
65          this.name = name;
66          this.protocolType = protocolType;
67          this.resourceMetaData = resourceMetaData;
68          this.ruleMetaData = ruleMetaData;
69          this.schemas = new ConcurrentHashMap<>(schemas.stream().collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each)));
70      }
71      
72      /**
73       * Get all schemas.
74       *
75       * @return all schemas
76       */
77      public Collection<ShardingSphereSchema> getAllSchemas() {
78          return schemas.values();
79      }
80      
81      /**
82       * Judge contains schema from database or not.
83       *
84       * @param schemaName schema name
85       * @return contains schema from database or not
86       */
87      public boolean containsSchema(final String schemaName) {
88          return schemas.containsKey(new ShardingSphereIdentifier(schemaName));
89      }
90      
91      /**
92       * Get schema.
93       *
94       * @param schemaName schema name
95       * @return schema
96       */
97      public ShardingSphereSchema getSchema(final String schemaName) {
98          return schemas.get(new ShardingSphereIdentifier(schemaName));
99      }
100     
101     /**
102      * Add schema.
103      *
104      * @param schema schema
105      */
106     public void addSchema(final ShardingSphereSchema schema) {
107         schemas.put(new ShardingSphereIdentifier(schema.getName()), schema);
108     }
109     
110     /**
111      * Drop schema.
112      *
113      * @param schemaName schema name
114      */
115     public void dropSchema(final String schemaName) {
116         schemas.remove(new ShardingSphereIdentifier(schemaName));
117     }
118     
119     /**
120      * Judge whether is completed.
121      *
122      * @return is completed or not
123      */
124     public boolean isComplete() {
125         return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnits().isEmpty();
126     }
127     
128     /**
129      * Judge whether contains data source.
130      *
131      * @return contains data source or not
132      */
133     public boolean containsDataSource() {
134         return !resourceMetaData.getStorageUnits().isEmpty();
135     }
136     
137     /**
138      * Reload rules.
139      */
140     public synchronized void reloadRules() {
141         Collection<ShardingSphereRule> toBeReloadedRules = ruleMetaData.getRules().stream()
142                 .filter(each -> each.getAttributes().findAttribute(MutableDataNodeRuleAttribute.class).isPresent()).collect(Collectors.toList());
143         RuleConfiguration ruleConfig = toBeReloadedRules.stream().map(ShardingSphereRule::getConfiguration).findFirst().orElse(null);
144         Collection<ShardingSphereRule> rules = new LinkedList<>(ruleMetaData.getRules());
145         toBeReloadedRules.stream().findFirst().ifPresent(optional -> {
146             rules.removeAll(toBeReloadedRules);
147             Map<String, DataSource> dataSources = resourceMetaData.getStorageUnits().entrySet().stream()
148                     .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
149             rules.add(optional.getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).reloadRule(ruleConfig, name, dataSources, rules));
150         });
151         ruleMetaData.getRules().clear();
152         ruleMetaData.getRules().addAll(rules);
153     }
154     
155     /**
156      * Check storage units existed.
157      *
158      * @param storageUnitNames storage unit names
159      */
160     public void checkStorageUnitsExisted(final Collection<String> storageUnitNames) {
161         Collection<String> notExistedDataSources = resourceMetaData.getNotExistedDataSources(storageUnitNames);
162         Collection<String> logicDataSources = ruleMetaData.getAttributes(DataSourceMapperRuleAttribute.class).stream()
163                 .flatMap(each -> each.getDataSourceMapper().keySet().stream()).collect(Collectors.toSet());
164         notExistedDataSources.removeIf(logicDataSources::contains);
165         ShardingSpherePreconditions.checkMustEmpty(notExistedDataSources, () -> new MissingRequiredStorageUnitsException(name, notExistedDataSources));
166     }
167     
168     /**
169      * Decorate rule configuration.
170      *
171      * @param ruleConfig rule configuration
172      * @return decorated rule configuration
173      */
174     @SuppressWarnings({"unchecked", "rawtypes"})
175     public RuleConfiguration decorateRuleConfiguration(final RuleConfiguration ruleConfig) {
176         Optional<RuleConfigurationDecorator> decorator = TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass());
177         if (!decorator.isPresent()) {
178             return ruleConfig;
179         }
180         Map<String, DataSource> dataSources = resourceMetaData.getStorageUnits().entrySet().stream()
181                 .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
182         return decorator.get().decorate(name, dataSources, ruleMetaData.getRules(), ruleConfig);
183     }
184 }