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.globalclock.rule;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.globalclock.api.config.GlobalClockRuleConfiguration;
22  import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
23  import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
24  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
27  import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
28  import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
29  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
30  import org.apache.shardingsphere.transaction.spi.TransactionHook;
31  
32  import java.util.Collection;
33  import java.util.Map;
34  import java.util.Optional;
35  import java.util.Properties;
36  
37  /**
38   * Global clock rule.
39   */
40  public final class GlobalClockRule implements GlobalRule {
41      
42      @Getter
43      private final GlobalClockRuleConfiguration configuration;
44      
45      public GlobalClockRule(final GlobalClockRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases) {
46          configuration = ruleConfig;
47          if (ruleConfig.isEnabled()) {
48              TypedSPILoader.getService(GlobalClockProvider.class, getGlobalClockProviderType(), configuration.getProps());
49              TypedSPILoader.getService(TransactionHook.class, "GLOBAL_CLOCK", createProperties(databases));
50          }
51      }
52      
53      private Properties createProperties(final Map<String, ShardingSphereDatabase> databases) {
54          Properties result = new Properties();
55          DatabaseType storageType = findStorageType(databases.values()).orElseGet(DatabaseTypeEngine::getDefaultStorageType);
56          result.setProperty("trunkType", storageType.getTrunkDatabaseType().orElse(storageType).getType());
57          result.setProperty("enabled", String.valueOf(configuration.isEnabled()));
58          result.setProperty("type", configuration.getType());
59          result.setProperty("provider", configuration.getProvider());
60          return result;
61      }
62      
63      private Optional<DatabaseType> findStorageType(final Collection<ShardingSphereDatabase> databases) {
64          return databases.stream()
65                  .flatMap(each -> each.getResourceMetaData().getStorageUnits().values().stream()).findFirst().map(StorageUnit::getStorageType);
66      }
67      
68      /**
69       * Get global clock provider type.
70       * 
71       * @return global clock provider type
72       */
73      public String getGlobalClockProviderType() {
74          return String.join(".", configuration.getType(), configuration.getProvider());
75      }
76      
77      @Override
78      public RuleAttributes getAttributes() {
79          return new RuleAttributes();
80      }
81  }