Skip to content

Commit

Permalink
For #28968, fix query for proxy_frontend_database_protocol_type
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang committed Nov 7, 2023
1 parent f9c5dd6 commit e2b293e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
import org.apache.shardingsphere.logging.constant.LoggingConstants;
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.util.LoggingUtils;
Expand Down Expand Up @@ -68,7 +69,7 @@ private String getConfigurationValue(final ShardingSphereMetaData metaData, fina
if (LoggingConstants.SQL_SHOW_VARIABLE_NAME.equalsIgnoreCase(variableName) || LoggingConstants.SQL_SIMPLE_VARIABLE_NAME.equalsIgnoreCase(variableName)) {
return getLoggingPropsValue(metaData, variableName);
}
return metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(variableName)).toString();
return getStringResult(metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(variableName)));
}

private String getLoggingPropsValue(final ShardingSphereMetaData metaData, final String variableName) {
Expand All @@ -85,7 +86,7 @@ private String getLoggingPropsValue(final ShardingSphereMetaData metaData, final
default:
}
}
return metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(variableName)).toString();
return getStringResult(metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(variableName)));
}

private boolean isTemporaryConfigurationKey(final String variableName) {
Expand All @@ -105,6 +106,13 @@ private String getSpecialValue(final ConnectionSession connectionSession, final
throw new UnsupportedVariableException(variableName);
}

private String getStringResult(final Object value) {
if (null == value) {
return "";
}
return value instanceof TypedSPI ? ((TypedSPI) value).getType().toString() : value.toString();
}

@Override
public Class<ShowDistVariableStatement> getType() {
return ShowDistVariableStatement.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
import org.apache.shardingsphere.logging.constant.LoggingConstants;
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.util.LoggingUtils;
Expand Down Expand Up @@ -50,11 +51,10 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereMetaData metaData, final ConnectionSession connectionSession, final ShowDistVariablesStatement sqlStatement) {
Collection<LocalDataQueryResultRow> result = ConfigurationPropertyKey.getKeyNames().stream().filter(each -> !"sql_show".equalsIgnoreCase(each) && !"sql_simple".equalsIgnoreCase(each)
&& null != metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(each)))
.map(each -> new LocalDataQueryResultRow(each.toLowerCase(), metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(each)).toString())).collect(Collectors.toList());
Collection<LocalDataQueryResultRow> result = ConfigurationPropertyKey.getKeyNames().stream().filter(each -> !"sql_show".equalsIgnoreCase(each) && !"sql_simple".equalsIgnoreCase(each))
.map(each -> new LocalDataQueryResultRow(each.toLowerCase(), getStringResult(metaData.getProps().getValue(ConfigurationPropertyKey.valueOf(each))))).collect(Collectors.toList());
result.addAll(TemporaryConfigurationPropertyKey.getKeyNames().stream()
.map(each -> new LocalDataQueryResultRow(each.toLowerCase(), metaData.getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.valueOf(each)).toString()))
.map(each -> new LocalDataQueryResultRow(each.toLowerCase(), getStringResult(metaData.getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.valueOf(each)))))
.collect(Collectors.toList()));
result.add(new LocalDataQueryResultRow(VariableEnum.CACHED_CONNECTIONS.name().toLowerCase(), connectionSession.getDatabaseConnectionManager().getConnectionSize()));
addLoggingPropsRows(metaData, result);
Expand All @@ -65,6 +65,13 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereMetaData
return result.stream().sorted(Comparator.comparing(each -> each.getCell(1).toString())).collect(Collectors.toList());
}

private String getStringResult(final Object value) {
if (null == value) {
return "";
}
return value instanceof TypedSPI ? ((TypedSPI) value).getType().toString() : value.toString();
}

private void addLoggingPropsRows(final ShardingSphereMetaData metaData, final Collection<LocalDataQueryResultRow> result) {
Optional<ShardingSphereLogger> sqlLogger = LoggingUtils.getSQLLogger(metaData.getGlobalRuleMetaData());
if (sqlLogger.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.logging.rule.LoggingRule;
import org.apache.shardingsphere.logging.rule.builder.DefaultLoggingRuleConfigurationBuilder;
import org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -72,7 +68,6 @@ void assertShowCachedConnections() {
@Test
void assertShowPropsVariable() {
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("sql-show", Boolean.TRUE.toString()))));
when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(new LoggingRule(new DefaultLoggingRuleConfigurationBuilder().build()))));
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(metaData, connectionSession, new ShowDistVariableStatement("SQL_SHOW"));
assertThat(actual.size(), is(1));
Expand All @@ -81,6 +76,17 @@ void assertShowPropsVariable() {
assertThat(row.getCell(2), is("true"));
}

@Test
void assertShowPropsVariableForTypedSPI() {
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("proxy-frontend-database-protocol-type", "MySQL"))));
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(metaData, connectionSession, new ShowDistVariableStatement("PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE"));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("proxy_frontend_database_protocol_type"));
assertThat(row.getCell(2), is("MySQL"));
}

@Test
void assertShowTemporaryPropsVariable() {
when(metaData.getTemporaryProps()).thenReturn(new TemporaryConfigurationProperties(PropertiesBuilder.build(new Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,19 @@ void assertGetColumns() {

@Test
void assertExecute() {
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("system_log_level", "INFO"))));
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("system-log-level", "INFO"))));
when(metaData.getTemporaryProps()).thenReturn(new TemporaryConfigurationProperties(PropertiesBuilder.build(new Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(new LoggingRule(new DefaultLoggingRuleConfigurationBuilder().build()))));
ShowDistVariablesExecutor executor = new ShowDistVariablesExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(metaData, connectionSession, mock(ShowDistVariablesStatement.class));
assertThat(actual.size(), is(20));
assertThat(actual.size(), is(21));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("agent_plugins_enabled"));
assertThat(row.getCell(2), is("true"));
}

@Test
void assertExecuteWithLike() {
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("system_log_level", "INFO"))));
when(metaData.getProps()).thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property("system-log-level", "INFO"))));
when(metaData.getTemporaryProps()).thenReturn(new TemporaryConfigurationProperties(PropertiesBuilder.build(new Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(new LoggingRule(new DefaultLoggingRuleConfigurationBuilder().build()))));
ShowDistVariablesExecutor executor = new ShowDistVariablesExecutor();
Expand Down

0 comments on commit e2b293e

Please sign in to comment.