diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java index 5b20912f3a91b6..83e95a32e5b6d4 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java @@ -43,7 +43,7 @@ public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) { this.encryptRuleConfig = encryptRuleConfiguration; encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration); } - + /** * Get encrypt table names. * diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java new file mode 100644 index 00000000000000..5ba1900a7867d1 --- /dev/null +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java @@ -0,0 +1,127 @@ +/* + * 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.core.util; + +import java.util.Properties; + +import org.apache.shardingsphere.api.config.RuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; +import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration; +import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; +import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.yaml.engine.YamlEngine; +import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.MasterSlaveRuleConfigurationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper; + +import lombok.extern.slf4j.Slf4j; + +/** + * Configuration printer class. + * + * @author sunbufu + */ +@Slf4j +public final class ConfigurationLogger { + + private ConfigurationLogger() { } + + /** + * log properties configuration. + * + * @param propsConfiguration properties configuration + */ + public static void log(final Properties propsConfiguration) { + if (null != propsConfiguration) { + log(propsConfiguration.getClass().getSimpleName(), YamlEngine.marshal(propsConfiguration)); + } + } + + /** + * log EncryptRuleConfiguration. + * + * @param encryptRuleConfiguration encryptRule configuration + */ + public static void log(final EncryptRuleConfiguration encryptRuleConfiguration) { + if (null != encryptRuleConfiguration) { + log(encryptRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new EncryptRuleConfigurationYamlSwapper().swap(encryptRuleConfiguration))); + } + } + + /** + * log ruleConfiguration. + * + * @param ruleConfiguration ruleConfiguration + */ + public static void log(final RuleConfiguration ruleConfiguration) { + if (null != ruleConfiguration) { + if (ruleConfiguration instanceof ShardingRuleConfiguration) { + ConfigurationLogger.log((ShardingRuleConfiguration) ruleConfiguration); + } else if (ruleConfiguration instanceof MasterSlaveRuleConfiguration) { + ConfigurationLogger.log((MasterSlaveRuleConfiguration) ruleConfiguration); + } else if (ruleConfiguration instanceof EncryptRuleConfiguration) { + ConfigurationLogger.log((EncryptRuleConfiguration) ruleConfiguration); + } + } + } + + /** + * log ShardingRuleConfiguration. + * + * @param shardingRuleConfiguration shardingRule configuration + */ + public static void log(final ShardingRuleConfiguration shardingRuleConfiguration) { + if (null != shardingRuleConfiguration) { + log(shardingRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new ShardingRuleConfigurationYamlSwapper().swap(shardingRuleConfiguration))); + } + } + + /** + * log MasterSlaveRuleConfiguration. + * + * @param masterSlaveRuleConfiguration masterSlaveRule configuration + */ + public static void log(final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration) { + if (null != masterSlaveRuleConfiguration) { + log(masterSlaveRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRuleConfiguration))); + } + } + + /** + * log AuthenticationConfiguration. + * + * @param authenticationConfiguration authentication configuration + */ + public static void log(final Authentication authenticationConfiguration) { + if (null != authenticationConfiguration) { + log(authenticationConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new AuthenticationYamlSwapper().swap(authenticationConfiguration))); + } + } + + /** + * log configuration log. + * + * @param base base node name + * @param yamlStr yaml string + */ + public static void log(final String base, final String yamlStr) { + log.info("{}\n{}", base, yamlStr); + } + +} diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java new file mode 100644 index 00000000000000..aeaba9f986ace6 --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java @@ -0,0 +1,196 @@ +/* + * 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.core.util; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import org.apache.shardingsphere.api.config.RuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration; +import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration; +import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; +import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration; +import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.rule.ProxyUser; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.slf4j.Logger; + +@RunWith(MockitoJUnitRunner.class) +public class ConfigurationLoggerTest { + + @Mock + private Logger log; + + @Before + public void setLog() throws NoSuchFieldException, IllegalAccessException { + Field field = ConfigurationLogger.class.getDeclaredField("log"); + setFinalStatic(field, log); + } + + private static void setFinalStatic(final Field field, final Object newValue) + throws NoSuchFieldException, IllegalAccessException { + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(null, newValue); + } + + public void assertEqualsWithLogInfo(final String base, final String yamlStr) { + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocationOnMock) { + Assert.assertEquals(base, invocationOnMock.getArgument(1)); + Assert.assertEquals(yamlStr, invocationOnMock.getArgument(2)); + return null; + } + }).when(log).info(anyString(), anyString(), anyString()); + } + + @Test + public void logPropsConfiguration() { + Properties properties = new Properties(); + properties.put("masterDataSourceName", "master_ds"); + properties.put("slaveDataSourceNames", Arrays.asList("slave_ds_0", "slave_ds_1")); + + String base = "Properties"; + String yamlStr = + "slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n" + "masterDataSourceName: master_ds\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(properties); + } + + @Test + public void logEncryptRuleConfiguration() { + EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(); + Properties properties = new Properties(); + properties.put("aes.key.value", "123456abc"); + EncryptorRuleConfiguration encryptorRuleConfiguration = + new EncryptorRuleConfiguration("aes", "user.user_name", properties); + encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration); + + String base = "EncryptRuleConfiguration"; + String yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n" + + " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(encryptRuleConfiguration); + } + + @Test + public void logShardingRuleConfiguration() { + ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration(); + TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}"); + shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration); + + String base = "ShardingRuleConfiguration"; + String yamlStr = + "tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(shardingRuleConfiguration); + } + + @Test + public void logMasterSlaveRuleConfiguration() { + MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = + new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1")); + + String base = "MasterSlaveRuleConfiguration"; + String yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + + "- slave_ds_0\n" + "- slave_ds_1\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(masterSlaveRuleConfiguration); + } + + @Test + public void logRuleConfiguration(){ + String base, yamlStr; + // EncryptRuleConfiguration + EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(); + Properties properties = new Properties(); + properties.put("aes.key.value", "123456abc"); + EncryptorRuleConfiguration encryptorRuleConfiguration = + new EncryptorRuleConfiguration("aes", "user.user_name", properties); + encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration); + + base = "EncryptRuleConfiguration"; + yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n" + + " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) encryptRuleConfiguration); + + // ShardingRuleConfiguration + ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration(); + TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}"); + shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration); + + base = "ShardingRuleConfiguration"; + yamlStr = + "tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) shardingRuleConfiguration); + + // MasterSlaveRuleConfiguration + MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = + new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1")); + + base = "MasterSlaveRuleConfiguration"; + yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + + "- slave_ds_0\n" + "- slave_ds_1\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) masterSlaveRuleConfiguration); + } + + @Test + public void logAuthenticationConfiguration() { + Authentication authentication = new Authentication(); + authentication.getUsers().put("root", new ProxyUser("123456", Collections.singletonList("sharding_db"))); + + String base = "Authentication"; + String yamlStr = "users:\n" + " root:\n" + " authorizedSchemas: sharding_db\n" + " password: '123456'\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(authentication); + } + + @Test + public void log() { + String base = "base"; + String yamlStr = "yamlStr"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(base, yamlStr); + } + +} \ No newline at end of file diff --git a/sharding-core/sharding-core-common/src/test/resources/logback-test.xml b/sharding-core/sharding-core-common/src/test/resources/logback-test.xml new file mode 100644 index 00000000000000..cade0c6eb570f4 --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/resources/logback-test.xml @@ -0,0 +1,34 @@ + + + + + + + [%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml b/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml index 9c8231634dd198..e180e2b6fec203 100644 --- a/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + + diff --git a/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml b/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml index 9c8231634dd198..e180e2b6fec203 100644 --- a/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml b/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml index 9c8231634dd198..e180e2b6fec203 100644 --- a/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + + diff --git a/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml b/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml index 9c8231634dd198..e180e2b6fec203 100644 --- a/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml +++ b/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + + diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java index 3e9aeddf4aceff..231ae91ac6eb84 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.parse.entry.EncryptSQLParseEntry; import org.apache.shardingsphere.core.rule.EncryptRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection; import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource; import org.apache.shardingsphere.spi.database.DatabaseType; @@ -74,6 +75,8 @@ public class EncryptDataSource extends AbstractUnsupportedOperationDataSource im @SneakyThrows public EncryptDataSource(final DataSource dataSource, final EncryptRuleConfiguration encryptRuleConfiguration, final Properties props) { + ConfigurationLogger.log(encryptRuleConfiguration); + ConfigurationLogger.log(props); this.dataSource = dataSource; databaseType = getDatabaseType(); encryptRule = new EncryptRule(encryptRuleConfiguration); diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java index 91c22cedafca93..85ec3ff33d62bc 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.parse.entry.MasterSlaveSQLParseEntry; import org.apache.shardingsphere.core.rule.MasterSlaveRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection; import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData; @@ -53,6 +54,8 @@ public class MasterSlaveDataSource extends AbstractDataSourceAdapter { public MasterSlaveDataSource(final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(masterSlaveRuleConfig); + ConfigurationLogger.log(props); cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap); this.masterSlaveRule = new MasterSlaveRule(masterSlaveRuleConfig); parseEngine = new MasterSlaveSQLParseEntry(getDatabaseType()); @@ -61,6 +64,8 @@ public MasterSlaveDataSource(final Map dataSourceMap, final public MasterSlaveDataSource(final Map dataSourceMap, final MasterSlaveRule masterSlaveRule, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(masterSlaveRule.getMasterSlaveRuleConfiguration()); + ConfigurationLogger.log(props); cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap); this.masterSlaveRule = masterSlaveRule; shardingProperties = new ShardingProperties(null == props ? new Properties() : props); diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java index be29ab34a251bf..66a1cef27ed39e 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java @@ -20,6 +20,7 @@ import com.google.common.base.Preconditions; import lombok.Getter; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter; import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection; @@ -49,6 +50,8 @@ public ShardingDataSource(final Map dataSourceMap, final Sha public ShardingDataSource(final Map dataSourceMap, final ShardingRule shardingRule, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(shardingRule.getShardingRuleConfig()); + ConfigurationLogger.log(props); checkDataSourceType(dataSourceMap); shardingContext = new ShardingContext(getDataSourceMap(), shardingRule, getDatabaseType(), props); } diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml b/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml index 9c8231634dd198..fe1d5aa5d835bf 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml +++ b/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml @@ -26,6 +26,7 @@ + diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml index acb15fdaf84aac..0802913124e90e 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml @@ -25,7 +25,8 @@ - + + diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml b/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml index c698274325144b..cade0c6eb570f4 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml +++ b/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml @@ -25,7 +25,8 @@ - + + diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java index 62a0d1f4275b42..fd7103c7298740 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.core.parse.entry.EncryptSQLParseEntry; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.EncryptRuleChangedEvent; import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter; @@ -37,6 +38,7 @@ * Encrypt schema. * * @author panjuan + * @author sunbufu */ @Getter public final class EncryptSchema extends LogicSchema { @@ -71,6 +73,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe @SneakyThrows public synchronized void renew(final EncryptRuleChangedEvent encryptRuleChangedEvent) { + ConfigurationLogger.log(encryptRuleChangedEvent.getEncryptRuleConfiguration()); encryptRule = new EncryptRule(encryptRuleChangedEvent.getEncryptRuleConfiguration()); parseEngine = new EncryptSQLParseEntry(LogicSchemas.getInstance().getDatabaseType(), metaData.getTable()); } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java index 32b3c4bda60202..954092bccb5725 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.core.parse.entry.MasterSlaveSQLParseEntry; import org.apache.shardingsphere.core.rule.MasterSlaveRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.event.DisabledStateChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.schema.OrchestrationShardingSchema; @@ -39,6 +40,7 @@ * Master-slave schema. * * @author panjuan + * @author sunbufu */ @Getter public final class MasterSlaveSchema extends LogicSchema { @@ -78,6 +80,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe public synchronized void renew(final MasterSlaveRuleChangedEvent masterSlaveRuleChangedEvent) { if (getName().equals(masterSlaveRuleChangedEvent.getShardingSchemaName())) { + ConfigurationLogger.log(masterSlaveRuleChangedEvent.getMasterSlaveRuleConfiguration()); masterSlaveRule = new OrchestrationMasterSlaveRule(masterSlaveRuleChangedEvent.getMasterSlaveRuleConfiguration()); } } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java index 26778675989cc1..41ab4ae93fcc99 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.core.parse.sql.statement.ddl.DropTableStatement; import org.apache.shardingsphere.core.rule.MasterSlaveRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.ShardingRuleChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.event.DisabledStateChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.schema.OrchestrationShardingSchema; @@ -51,6 +52,7 @@ * @author panjuan * @author zhaojun * @author wangkai + * @author sunbufu */ @Getter public final class ShardingSchema extends LogicSchema { @@ -83,6 +85,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe public synchronized void renew(final ShardingRuleChangedEvent shardingRuleChangedEvent) { if (getName().equals(shardingRuleChangedEvent.getShardingSchemaName())) { + ConfigurationLogger.log(shardingRuleChangedEvent.getShardingRuleConfiguration()); shardingRule = new OrchestrationShardingRule(shardingRuleChangedEvent.getShardingRuleConfiguration(), getDataSources().keySet()); } } diff --git a/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java b/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java index d7556d7cf17c58..05808f28ec518c 100644 --- a/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java +++ b/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java @@ -19,10 +19,12 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.commons.collections4.CollectionUtils; import org.apache.shardingsphere.api.config.RuleConfiguration; import org.apache.shardingsphere.core.config.DataSourceConfiguration; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.core.yaml.config.common.YamlAuthenticationConfiguration; import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper; import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper; @@ -55,12 +57,13 @@ * @author zhangliang * @author wangkai * @author panjuan + * @author sunbufu */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class Bootstrap { - + private static final int DEFAULT_PORT = 3307; - + /** * Main entrance. * @@ -69,6 +72,7 @@ public final class Bootstrap { */ public static void main(final String[] args) throws IOException { ShardingConfiguration shardingConfig = new ShardingConfigurationLoader().load(); + logRuleConfigurationMap(getRuleConfiguration(shardingConfig.getRuleConfigurationMap()).values()); int port = getPort(args); if (null == shardingConfig.getServerConfiguration().getOrchestration()) { startWithoutRegistryCenter(shardingConfig.getRuleConfigurationMap(), shardingConfig.getServerConfiguration().getAuthentication(), shardingConfig.getServerConfiguration().getProps(), port); @@ -76,7 +80,7 @@ public static void main(final String[] args) throws IOException { startWithRegistryCenter(shardingConfig.getServerConfiguration(), shardingConfig.getRuleConfigurationMap().keySet(), shardingConfig.getRuleConfigurationMap(), port); } } - + private static int getPort(final String[] args) { if (0 == args.length) { return DEFAULT_PORT; @@ -87,15 +91,15 @@ private static int getPort(final String[] args) { return DEFAULT_PORT; } } - - private static void startWithoutRegistryCenter(final Map ruleConfigs, + + private static void startWithoutRegistryCenter(final Map ruleConfigs, final YamlAuthenticationConfiguration authentication, final Properties prop, final int port) { ShardingProxyContext.getInstance().init(getAuthentication(authentication), prop); LogicSchemas.getInstance().init(getDataSourceParameterMap(ruleConfigs), getRuleConfiguration(ruleConfigs)); initOpenTracing(); ShardingProxy.getInstance().start(port); } - + private static void startWithRegistryCenter(final YamlProxyServerConfiguration serverConfig, final Collection shardingSchemaNames, final Map ruleConfigs, final int port) { try (ShardingOrchestrationFacade shardingOrchestrationFacade = new ShardingOrchestrationFacade( @@ -107,7 +111,7 @@ private static void startWithRegistryCenter(final YamlProxyServerConfiguration s ShardingProxy.getInstance().start(port); } } - + private static Map> getSchemaDataSourceParameterMap(final ShardingOrchestrationFacade shardingOrchestrationFacade) { Map> result = new LinkedHashMap<>(); for (String each : shardingOrchestrationFacade.getConfigService().getAllShardingSchemaNames()) { @@ -115,7 +119,7 @@ private static Map> getSchemaDataSo } return result; } - + private static Map getSchemaRules(final ShardingOrchestrationFacade shardingOrchestrationFacade) { Map result = new LinkedHashMap<>(); for (String each : shardingOrchestrationFacade.getConfigService().getAllShardingSchemaNames()) { @@ -129,23 +133,23 @@ private static Map getSchemaRules(final ShardingOrche } return result; } - + private static void initShardingOrchestrationFacade( final YamlProxyServerConfiguration serverConfig, final Map ruleConfigs, final ShardingOrchestrationFacade shardingOrchestrationFacade) { if (ruleConfigs.isEmpty()) { shardingOrchestrationFacade.init(); } else { - shardingOrchestrationFacade.init(getDataSourceConfigurationMap(ruleConfigs), + shardingOrchestrationFacade.init(getDataSourceConfigurationMap(ruleConfigs), getRuleConfiguration(ruleConfigs), getAuthentication(serverConfig.getAuthentication()), serverConfig.getProps()); } } - + private static void initOpenTracing() { if (ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.PROXY_OPENTRACING_ENABLED)) { ShardingTracer.init(); } } - + private static Map> getDataSourceConfigurationMap(final Map ruleConfigs) { Map> result = new LinkedHashMap<>(); for (Entry entry : ruleConfigs.entrySet()) { @@ -161,7 +165,7 @@ private static Map> getDataSourcePa } return result; } - + private static Map getRuleConfiguration(final Map localRuleConfigs) { Map result = new HashMap<>(); for (Entry entry : localRuleConfigs.entrySet()) { @@ -175,8 +179,22 @@ private static Map getRuleConfiguration(final Map ruleConfigurations) { + if (CollectionUtils.isNotEmpty(ruleConfigurations)) { + for (RuleConfiguration each : ruleConfigurations) { + ConfigurationLogger.log(each); + } + } + } + } diff --git a/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java b/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java index c6c78b53b37b2a..544a8cb9c94229 100644 --- a/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java +++ b/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java @@ -17,16 +17,19 @@ package org.apache.shardingsphere.shardingproxy.context; -import com.google.common.eventbus.Subscribe; -import lombok.Getter; +import java.util.Properties; + import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.eventbus.ShardingOrchestrationEventBus; import org.apache.shardingsphere.orchestration.internal.registry.config.event.AuthenticationChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.config.event.PropertiesChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.event.CircuitStateChangedEvent; -import java.util.Properties; +import com.google.common.eventbus.Subscribe; + +import lombok.Getter; /** * Context of Sharding-Proxy. @@ -34,6 +37,7 @@ * @author chenqingyang * @author panjuan * @author zhangliang + * @author sunbufu */ @Getter public final class ShardingProxyContext { @@ -66,8 +70,8 @@ public static ShardingProxyContext getInstance() { * @param props properties */ public void init(final Authentication authentication, final Properties props) { - this.authentication = authentication; - shardingProperties = new ShardingProperties(props); + setAuthentication(authentication); + setProperties(props); } /** @@ -77,7 +81,7 @@ public void init(final Authentication authentication, final Properties props) { */ @Subscribe public synchronized void renew(final PropertiesChangedEvent event) { - shardingProperties = new ShardingProperties(event.getProps()); + setProperties(event.getProps()); } /** @@ -87,7 +91,7 @@ public synchronized void renew(final PropertiesChangedEvent event) { */ @Subscribe public synchronized void renew(final AuthenticationChangedEvent event) { - authentication = event.getAuthentication(); + setAuthentication(event.getAuthentication()); } /** @@ -99,4 +103,24 @@ public synchronized void renew(final AuthenticationChangedEvent event) { public synchronized void renew(final CircuitStateChangedEvent event) { isCircuitBreak = event.isCircuitBreak(); } + + /** + * set authentication. + * + * @param authentication new authentication + */ + public void setAuthentication(final Authentication authentication) { + ConfigurationLogger.log(authentication); + this.authentication = authentication; + } + + /** + * set new ShardingProperties with props. + * + * @param props new props + */ + public void setProperties(final Properties props) { + ConfigurationLogger.log(props); + this.shardingProperties = new ShardingProperties(props); + } } diff --git a/sharding-proxy/sharding-proxy-common/src/test/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContextTest.java b/sharding-proxy/sharding-proxy-common/src/test/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContextTest.java index 23332dd62a072f..83b03da5b7d916 100644 --- a/sharding-proxy/sharding-proxy-common/src/test/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContextTest.java +++ b/sharding-proxy/sharding-proxy-common/src/test/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContextTest.java @@ -30,6 +30,7 @@ import java.util.Properties; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -82,4 +83,23 @@ public void assertRenewCircuitState() { assertTrue(ShardingProxyContext.getInstance().isCircuitBreak()); ShardingOrchestrationEventBus.getInstance().post(new CircuitStateChangedEvent(false)); } + + @Test + public void setAuthentication() { + Authentication authentication = new Authentication(); + ProxyUser proxyUser = new ProxyUser("root", Collections.singletonList("")); + authentication.getUsers().put("root", proxyUser); + + ShardingProxyContext.getInstance().setAuthentication(authentication); + assertEquals(ShardingProxyContext.getInstance().getAuthentication(), authentication); + } + + @Test + public void setProps(){ + Properties props = new Properties(); + props.put("sql.show", false); + + ShardingProxyContext.getInstance().setProperties(props); + assertEquals(ShardingProxyContext.getInstance().getShardingProperties().getProps(), props); + } } diff --git a/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml b/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml index c698274325144b..cade0c6eb570f4 100644 --- a/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml +++ b/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml @@ -25,7 +25,8 @@ - + + diff --git a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml index 2ac59ae007256f..038ffc08687bbf 100644 --- a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml @@ -27,7 +27,8 @@ - + + diff --git a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml index acb15fdaf84aac..0802913124e90e 100644 --- a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml @@ -25,7 +25,8 @@ - + + diff --git a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml index 2ac59ae007256f..038ffc08687bbf 100644 --- a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml @@ -27,7 +27,8 @@ - + + diff --git a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml index acb15fdaf84aac..cff63bb15ba773 100644 --- a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml @@ -25,6 +25,7 @@ + diff --git a/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml index e887b226ef4a82..b03d65953b492f 100644 --- a/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + + diff --git a/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml index c698274325144b..cade0c6eb570f4 100644 --- a/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml @@ -25,7 +25,8 @@ - + + diff --git a/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml index 28610d7b45dd26..2fec9795347337 100644 --- a/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml @@ -26,7 +26,8 @@ - + +