Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log configuration from reg center into log files #2701

Merged
merged 8 commits into from
Jul 15, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) {
this.encryptRuleConfig = encryptRuleConfiguration;
encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration);
}

/**
* Get encrypt table names.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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() { }
sunbufu marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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) {
sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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) {
sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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);
}

sunbufu marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -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
sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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"));

sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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);

sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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";
sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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
sunbufu marked this conversation as resolved.
Show resolved Hide resolved
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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.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.
-->

<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache.shardingsphere" level="info" additivity="false">
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<appender-ref ref="console"/>
</logger>
<logger name="org.apache.shardingsphere.core.execute.sql.execute.threadlocal.ExecutorExceptionHandler" level="off" />

<logger name="org.apache.shardingsphere.core.util.ConfigurationLogger" level="off" />

<root>
<level value="error" />
<appender-ref ref="console" />
Expand Down
Loading