This is an automated email from the ASF dual-hosted git repository.

zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0455fce5f82 Refactor DatabaseTimeService (#23733)
0455fce5f82 is described below

commit 0455fce5f829539ac1cbd5bc9f3984180f9f7467
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 27 00:14:30 2023 +0800

    Refactor DatabaseTimeService (#23733)
    
    * Refactor DatabaseTimeService
    
    * Refactor DatabaseTimeService
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 -
 .../user-manual/error-code/sql-error-code.en.md    |  1 -
 .../timeservice/spi/ShardingSphereTimeService.java |  3 +-
 kernel/time-service/type/database/pom.xml          |  6 ++
 .../type/database/DatabaseTimeService.java         | 28 ++++++---
 .../DatabaseDatetimeServiceConfiguration.java      | 70 ----------------------
 ...DatetimeConfigurationFileNotFoundException.java | 33 ----------
 .../type/database/DatabaseTimeServiceTest.java     | 16 +++--
 .../DatabaseTimeServiceConfigurationTest.java      | 34 -----------
 .../test/resources/datetime-database-config.yaml   | 23 -------
 .../timeservice/type/system/SystemTimeService.java |  5 ++
 .../type/system/SystemTimeServiceTest.java         | 10 ++--
 12 files changed, 50 insertions(+), 180 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md 
b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 6ca80d7875d..97e97eba940 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -31,7 +31,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | --------- |-------------| ------ |
 | HY004     | 11000       | Invalid value \`%s\`. |
 | HY004     | 11001       | Unsupported conversion data type \`%s\` for value 
\`%s\`. |
-| HY000     | 11002       | Can not find \`%s\` file for datetime initialize. |
 | HY004     | 11010       | Unsupported conversion stream charset \`%s\`. |
 
 ### 语法
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md 
b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 6e3f28427d9..de5fbd36640 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -31,7 +31,6 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | --------- | ----------- | ------ |
 | HY004     | 11000       | Invalid value \`%s\`. |
 | HY004     | 11001       | Unsupported conversion data type \`%s\` for value 
\`%s\`. |
-| HY000     | 11002       | Can not find \`%s\` file for datetime initialize. |
 | HY004     | 11010       | Unsupported conversion stream charset \`%s\`. |
 
 ### Syntax
diff --git 
a/kernel/time-service/api/src/main/java/org/apache/shardingsphere/timeservice/spi/ShardingSphereTimeService.java
 
b/kernel/time-service/api/src/main/java/org/apache/shardingsphere/timeservice/spi/ShardingSphereTimeService.java
index 804deae4933..538cf4a0264 100644
--- 
a/kernel/time-service/api/src/main/java/org/apache/shardingsphere/timeservice/spi/ShardingSphereTimeService.java
+++ 
b/kernel/time-service/api/src/main/java/org/apache/shardingsphere/timeservice/spi/ShardingSphereTimeService.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.timeservice.spi;
 
+import org.apache.shardingsphere.infra.util.spi.lifecycle.SPIPostProcessor;
 import org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPI;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
 
@@ -25,7 +26,7 @@ import java.util.Date;
 /**
  * ShardingSphere time service.
  */
-public interface ShardingSphereTimeService extends TypedSPI, RequiredSPI {
+public interface ShardingSphereTimeService extends TypedSPI, RequiredSPI, 
SPIPostProcessor {
     
     /**
      * Get datetime.
diff --git a/kernel/time-service/type/database/pom.xml 
b/kernel/time-service/type/database/pom.xml
index 3fcdacd51bf..9430d85f992 100644
--- a/kernel/time-service/type/database/pom.xml
+++ b/kernel/time-service/type/database/pom.xml
@@ -49,5 +49,11 @@
             <artifactId>HikariCP</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-util</artifactId>
+            <version>5.3.2-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeService.java
 
b/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeService.java
index fde5461903f..21a250d343c 100644
--- 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeService.java
+++ 
b/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeService.java
@@ -17,32 +17,46 @@
 
 package org.apache.shardingsphere.timeservice.type.database;
 
-import 
org.apache.shardingsphere.timeservice.type.database.config.DatabaseDatetimeServiceConfiguration;
-import 
org.apache.shardingsphere.timeservice.type.database.exception.DatetimeLoadingException;
-import 
org.apache.shardingsphere.timeservice.type.database.provider.DatetimeLoadingSQLProvider;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
-import org.apache.shardingsphere.timeservice.spi.ShardingSphereTimeService;
+import 
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
 import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
+import org.apache.shardingsphere.timeservice.spi.ShardingSphereTimeService;
+import 
org.apache.shardingsphere.timeservice.type.database.exception.DatetimeLoadingException;
+import 
org.apache.shardingsphere.timeservice.type.database.provider.DatetimeLoadingSQLProvider;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.Collections;
 import java.util.Date;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.stream.Collectors;
 
 /**
  * Database time service.
  */
 public final class DatabaseTimeService implements ShardingSphereTimeService {
     
-    private final DatabaseDatetimeServiceConfiguration timeServiceConfig = 
DatabaseDatetimeServiceConfiguration.getInstance();
+    private DataSource dataSource;
+    
+    private DatabaseType storageType;
+    
+    @Override
+    public void init(final Properties props) {
+        dataSource = DataSourcePoolCreator.create(new 
YamlDataSourceConfigurationSwapper().swapToDataSourceProperties(
+                props.entrySet().stream().collect(Collectors.toMap(entry -> 
entry.getKey().toString(), Entry::getValue, (key, value) -> value))));
+        storageType = 
DatabaseTypeEngine.getStorageType(Collections.singleton(dataSource));
+    }
     
     @Override
     public Date getDatetime() {
         try {
-            return loadDatetime(timeServiceConfig.getDataSource(),
-                    
TypedSPIRegistry.getService(DatetimeLoadingSQLProvider.class, 
DatabaseTypeEngine.getTrunkDatabaseTypeName(timeServiceConfig.getStorageType())).getDatetimeLoadingSQL());
+            return loadDatetime(dataSource, 
TypedSPIRegistry.getService(DatetimeLoadingSQLProvider.class, 
DatabaseTypeEngine.getTrunkDatabaseTypeName(storageType)).getDatetimeLoadingSQL());
         } catch (final SQLException ex) {
             throw new DatetimeLoadingException(ex);
         }
diff --git 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseDatetimeServiceConfiguration.java
 
b/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseDatetimeServiceConfiguration.java
deleted file mode 100644
index ddf0fa3fbd8..00000000000
--- 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseDatetimeServiceConfiguration.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.timeservice.type.database.config;
-
-import lombok.Getter;
-import 
org.apache.shardingsphere.timeservice.type.database.exception.DatetimeConfigurationFileNotFoundException;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
-import 
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
-import org.yaml.snakeyaml.Yaml;
-
-import javax.sql.DataSource;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
-import java.util.Map;
-
-/**
- * Database datetime service configuration.
- */
-@Getter
-public final class DatabaseDatetimeServiceConfiguration {
-    
-    private static final DatabaseDatetimeServiceConfiguration INSTANCE = new 
DatabaseDatetimeServiceConfiguration();
-    
-    private static final String CONFIG_FILE = "datetime-database-config.yaml";
-    
-    private final DataSource dataSource;
-    
-    private final DatabaseType storageType;
-    
-    private DatabaseDatetimeServiceConfiguration() {
-        dataSource = DataSourcePoolCreator.create(new 
YamlDataSourceConfigurationSwapper().swapToDataSourceProperties(loadDataSourceConfiguration()));
-        storageType = 
DatabaseTypeEngine.getStorageType(Collections.singletonList(dataSource));
-    }
-    
-    @SuppressWarnings("unchecked")
-    private Map<String, Object> loadDataSourceConfiguration() {
-        try (InputStream inputStream = 
getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {
-            return new Yaml().loadAs(inputStream, Map.class);
-        } catch (final IOException ex) {
-            throw new DatetimeConfigurationFileNotFoundException(CONFIG_FILE);
-        }
-    }
-    
-    /**
-     * Get time service configuration instance.
-     * 
-     * @return time service configuration
-     */
-    public static DatabaseDatetimeServiceConfiguration getInstance() {
-        return INSTANCE;
-    }
-}
diff --git 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/exception/DatetimeConfigurationFileNotFoundException.java
 
b/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/exception/DatetimeConfigurationFileNotFoundException.java
deleted file mode 100644
index c3fbc92e874..00000000000
--- 
a/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/exception/DatetimeConfigurationFileNotFoundException.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.timeservice.type.database.exception;
-
-import org.apache.shardingsphere.infra.exception.DataSQLException;
-import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
-
-/**
- * Datetime configuration file not found exception.
- */
-public final class DatetimeConfigurationFileNotFoundException extends 
DataSQLException {
-    
-    private static final long serialVersionUID = 4820838154441059833L;
-    
-    public DatetimeConfigurationFileNotFoundException(final String 
configurationFile) {
-        super(XOpenSQLState.GENERAL_ERROR, 2, "Can not find `%s` file for 
datetime initialize.", configurationFile);
-    }
-}
diff --git 
a/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeServiceTest.java
 
b/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeServiceTest.java
index b7ba68ee423..25ad5b4fc66 100644
--- 
a/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeServiceTest.java
+++ 
b/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimeServiceTest.java
@@ -17,10 +17,13 @@
 
 package org.apache.shardingsphere.timeservice.type.database;
 
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import org.apache.shardingsphere.timeservice.spi.ShardingSphereTimeService;
 import org.junit.Test;
 
-import java.util.Date;
+import java.util.Properties;
 
 import static org.junit.Assert.assertTrue;
 
@@ -28,8 +31,13 @@ public final class DatabaseTimeServiceTest {
     
     @Test
     public void assertGetDatetime() {
-        Date currentDate = new Date();
-        ShardingSphereTimeService datetimeService = new DatabaseTimeService();
-        assertTrue(datetimeService.getDatetime().getTime() >= 
currentDate.getTime());
+        Properties props = PropertiesBuilder.build(
+                new Property("dataSourceClassName", 
"com.zaxxer.hikari.HikariDataSource"),
+                new Property("jdbcUrl", 
"jdbc:h2:mem:foo_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"),
+                new Property("username", "sa"),
+                new Property("password", ""),
+                new Property("maximumPoolSize", "1"));
+        long currentTime = System.currentTimeMillis();
+        
assertTrue(TypedSPIRegistry.getService(ShardingSphereTimeService.class, 
"Database", props).getDatetime().getTime() >= currentTime);
     }
 }
diff --git 
a/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseTimeServiceConfigurationTest.java
 
b/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseTimeServiceConfigurationTest.java
deleted file mode 100644
index 55147b2911d..00000000000
--- 
a/kernel/time-service/type/database/src/test/java/org/apache/shardingsphere/timeservice/type/database/config/DatabaseTimeServiceConfigurationTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.timeservice.type.database.config;
-
-import com.zaxxer.hikari.HikariDataSource;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-public final class DatabaseTimeServiceConfigurationTest {
-    
-    @Test
-    public void assertGetInstance() {
-        
assertThat(DatabaseDatetimeServiceConfiguration.getInstance().getStorageType().getType(),
 is("H2"));
-        
assertThat(DatabaseDatetimeServiceConfiguration.getInstance().getDataSource(), 
instanceOf(HikariDataSource.class));
-    }
-}
diff --git 
a/kernel/time-service/type/database/src/test/resources/datetime-database-config.yaml
 
b/kernel/time-service/type/database/src/test/resources/datetime-database-config.yaml
deleted file mode 100644
index 69a51962608..00000000000
--- 
a/kernel/time-service/type/database/src/test/resources/datetime-database-config.yaml
+++ /dev/null
@@ -1,23 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License.  You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-dataSourceClassName: com.zaxxer.hikari.HikariDataSource
-driverClassName: org.h2.Driver
-jdbcUrl: 
jdbc:h2:mem:db_write;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
-username: sa
-password:
-maxTotal: 100
diff --git 
a/kernel/time-service/type/system/src/main/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeService.java
 
b/kernel/time-service/type/system/src/main/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeService.java
index d67c17a346e..efa7357a2ac 100644
--- 
a/kernel/time-service/type/system/src/main/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeService.java
+++ 
b/kernel/time-service/type/system/src/main/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeService.java
@@ -20,12 +20,17 @@ package org.apache.shardingsphere.timeservice.type.system;
 import org.apache.shardingsphere.timeservice.spi.ShardingSphereTimeService;
 
 import java.util.Date;
+import java.util.Properties;
 
 /**
  * System time service.
  */
 public final class SystemTimeService implements ShardingSphereTimeService {
     
+    @Override
+    public void init(final Properties props) {
+    }
+    
     @Override
     public Date getDatetime() {
         return new Date();
diff --git 
a/kernel/time-service/type/system/src/test/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeServiceTest.java
 
b/kernel/time-service/type/system/src/test/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeServiceTest.java
index 8766d3cf335..ba973f11979 100644
--- 
a/kernel/time-service/type/system/src/test/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeServiceTest.java
+++ 
b/kernel/time-service/type/system/src/test/java/org/apache/shardingsphere/timeservice/type/system/SystemTimeServiceTest.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.timeservice.type.system;
 
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import org.apache.shardingsphere.timeservice.spi.ShardingSphereTimeService;
 import org.junit.Test;
 
 import static org.junit.Assert.assertTrue;
@@ -25,11 +27,7 @@ public final class SystemTimeServiceTest {
     
     @Test
     public void assertGetDatetime() {
-        assertTrue(new SystemTimeService().getDatetime().getTime() <= 
System.currentTimeMillis());
-    }
-    
-    @Test
-    public void assertIsDefault() {
-        assertTrue(new SystemTimeService().isDefault());
+        long currentTime = System.currentTimeMillis();
+        
assertTrue(TypedSPIRegistry.getService(ShardingSphereTimeService.class, 
null).getDatetime().getTime() >= currentTime);
     }
 }

Reply via email to