From 217819b8ac15b4c3e35ccf0ba6d4182343357f77 Mon Sep 17 00:00:00 2001 From: RaigorJiang Date: Mon, 13 Nov 2023 15:39:21 +0800 Subject: [PATCH] Fix set dist variable for proxy_frontend_database_protocol_type --- .../ral/updatable/SetDistVariableUpdater.java | 6 +++++- .../ral/updatable/SetDistVariableUpdaterTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdater.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdater.java index f65f4468a9a37..b3ccf4a83a581 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdater.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdater.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.props.TypedPropertyKey; import org.apache.shardingsphere.infra.props.TypedPropertyValue; import org.apache.shardingsphere.infra.props.exception.TypedPropertyValueException; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; import org.apache.shardingsphere.logging.constant.LoggingConstants; import org.apache.shardingsphere.logging.util.LoggingUtils; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -82,7 +83,10 @@ private void handleConfigurationProperty(final TypedPropertyKey propertyKey, fin private Object getValue(final TypedPropertyKey propertyKey, final String value) { try { Object propertyValue = new TypedPropertyValue(propertyKey, value).getValue(); - return Enum.class.isAssignableFrom(propertyKey.getType()) ? propertyValue.toString() : propertyValue; + if (Enum.class.isAssignableFrom(propertyKey.getType())) { + return propertyValue.toString(); + } + return TypedSPI.class.isAssignableFrom(propertyKey.getType()) ? ((TypedSPI) propertyValue).getType().toString() : propertyValue; } catch (final TypedPropertyValueException ignored) { throw new InvalidValueException(value); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdaterTest.java index 9168951fcf30d..4ff14fd825037 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/SetDistVariableUpdaterTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; +import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; @@ -44,6 +45,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -79,6 +81,18 @@ void assertExecuteWithTemporaryConfigurationKey() { assertThat(contextManager.getMetaDataContexts().getMetaData().getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.PROXY_META_DATA_COLLECTOR_ENABLED), is(false)); } + @Test + void assertExecuteWithTypedSPI() { + SetDistVariableStatement statement = new SetDistVariableStatement("proxy_frontend_database_protocol_type", "MySQL"); + SetDistVariableUpdater updater = new SetDistVariableUpdater(); + ContextManager contextManager = mockContextManager(); + when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); + updater.executeUpdate(connectionSession, statement); + Object actualValue = contextManager.getMetaDataContexts().getMetaData().getProps().getProps().get("proxy-frontend-database-protocol-type"); + assertThat(actualValue.toString(), is("MySQL")); + assertInstanceOf(MySQLDatabaseType.class, contextManager.getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE)); + } + @Test void assertExecuteWithSystemLogLevel() { SetDistVariableStatement statement = new SetDistVariableStatement("system_log_level", "debug");