Skip to content

Commit

Permalink
Refactor DatabaseAwareQueryableRALExecutor (#29859)
Browse files Browse the repository at this point in the history
* Rename DataSourcePoolPropertiesValidator

* Move DatabaseAwareQueryableRALExecutor

* Refactor DatabaseAwareQueryableRALExecutor
  • Loading branch information
terrymanu authored Jan 25, 2024
1 parent 5e6575b commit ed3ac53
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package org.apache.shardingsphere.distsql.handler.type.ral.query;
package org.apache.shardingsphere.distsql.handler.type.ral.query.aware;

import org.apache.shardingsphere.distsql.handler.type.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.QueryableRALStatement;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package org.apache.shardingsphere.distsql.handler.type.ral.query;
package org.apache.shardingsphere.distsql.handler.type.ral.query.aware;

import org.apache.shardingsphere.distsql.handler.type.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.QueryableRALStatement;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;

Expand All @@ -28,9 +29,9 @@
public interface DatabaseAwareQueryableRALExecutor<T extends QueryableRALStatement> extends QueryableRALExecutor<T> {

/**
* Set current database.
* Set database.
*
* @param currentDatabase current database
* @param database database
*/
void setCurrentDatabase(ShardingSphereDatabase currentDatabase);
void setDatabase(ShardingSphereDatabase database);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package org.apache.shardingsphere.distsql.handler.type.ral.query;
package org.apache.shardingsphere.distsql.handler.type.ral.query.aware;

import org.apache.shardingsphere.distsql.handler.type.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.QueryableRALStatement;
import org.apache.shardingsphere.infra.instance.InstanceContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.datasource.pool.props.validator.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;

import java.util.Collection;
import java.util.Map;

/**
* Data source pool properties validate handler.
* Data source pool properties validator.
*/
public final class DataSourcePoolPropertiesValidateHandler {
public final class DataSourcePoolPropertiesValidator {

/**
* Validate data source properties map.
Expand All @@ -37,7 +36,7 @@ public final class DataSourcePoolPropertiesValidateHandler {
* @throws InvalidStorageUnitsException invalid storage units exception
*/
public void validate(final Map<String, DataSourcePoolProperties> propsMap) {
Collection<String> errorMessages = DataSourcePoolPropertiesValidator.validate(propsMap);
Collection<String> errorMessages = org.apache.shardingsphere.infra.datasource.pool.props.validator.DataSourcePoolPropertiesValidator.validate(propsMap);
ShardingSpherePreconditions.checkState(errorMessages.isEmpty(), () -> new InvalidStorageUnitsException(errorMessages));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.data.pipeline.core.job.api.TransmissionJobAPI;
import org.apache.shardingsphere.data.pipeline.scenario.migration.api.MigrationJobAPI;
import org.apache.shardingsphere.distsql.handler.type.ral.update.UpdatableRALExecutor;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidateHandler;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
Expand All @@ -46,7 +46,7 @@ public final class RegisterMigrationSourceStorageUnitExecutor implements Updatab

private final MigrationJobAPI jobAPI = (MigrationJobAPI) TypedSPILoader.getService(TransmissionJobAPI.class, "MIGRATION");

private final DataSourcePoolPropertiesValidateHandler validateHandler = new DataSourcePoolPropertiesValidateHandler();
private final DataSourcePoolPropertiesValidator validateHandler = new DataSourcePoolPropertiesValidator();

@Override
public void executeUpdate(final RegisterMigrationSourceStorageUnitStatement sqlStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.QueryableRALStatement;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
Expand Down Expand Up @@ -74,7 +74,7 @@ private MergedResult getMergedResult(final QueryableRALExecutor<T> executor) {
((InstanceContextAwareQueryableRALExecutor<T>) executor).setInstanceContext(ProxyContext.getInstance().getContextManager().getInstanceContext());
}
if (executor instanceof DatabaseAwareQueryableRALExecutor) {
((DatabaseAwareQueryableRALExecutor<T>) executor).setCurrentDatabase(ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession)));
((DatabaseAwareQueryableRALExecutor<T>) executor).setDatabase(ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession)));
}
if (executor instanceof ConnectionSizeAwareQueryableRALExecutor) {
((ConnectionSizeAwareQueryableRALExecutor<T>) executor).setConnectionSize(connectionSession.getDatabaseConnectionManager().getConnectionSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.type.ral.query.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.export.ExportDatabaseConfigurationStatement;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
Expand All @@ -34,7 +34,7 @@
@Setter
public final class ExportDatabaseConfigurationExecutor implements DatabaseAwareQueryableRALExecutor<ExportDatabaseConfigurationStatement> {

private ShardingSphereDatabase currentDatabase;
private ShardingSphereDatabase database;

@Override
public Collection<String> getColumnNames() {
Expand All @@ -43,7 +43,7 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final ExportDatabaseConfigurationStatement sqlStatement, final ShardingSphereMetaData metaData) {
String exportedData = ExportUtils.generateExportDatabaseData(currentDatabase);
String exportedData = ExportUtils.generateExportDatabaseData(database);
if (!sqlStatement.getFilePath().isPresent()) {
return Collections.singleton(new LocalDataQueryResultRow(exportedData));
}
Expand All @@ -53,8 +53,8 @@ public Collection<LocalDataQueryResultRow> getRows(final ExportDatabaseConfigura
}

@Override
public void setCurrentDatabase(final ShardingSphereDatabase currentDatabase) {
this.currentDatabase = currentDatabase;
public void setDatabase(final ShardingSphereDatabase database) {
this.database = database;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.type.ral.query.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodeInfoStatement;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.type.ral.query.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodeModeStatement;
import org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
import org.apache.shardingsphere.infra.instance.InstanceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.type.ral.query.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.InstanceContextAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodesStatement;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.logging.util.LoggingUtils;
import org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.DistSQLVariable;
import org.apache.shardingsphere.distsql.handler.type.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.ConnectionSizeAwareQueryableRALExecutor;

import java.util.Arrays;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.util.LoggingUtils;
import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.DistSQLVariable;
import org.apache.shardingsphere.distsql.handler.type.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.type.ral.query.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.ral.query.aware.DatabaseAwareQueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowTableMetaDataStatement;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
Expand All @@ -41,7 +41,7 @@
@Setter
public final class ShowTableMetaDataExecutor implements DatabaseAwareQueryableRALExecutor<ShowTableMetaDataStatement> {

private ShardingSphereDatabase currentDatabase;
private ShardingSphereDatabase database;

@Override
public Collection<String> getColumnNames() {
Expand All @@ -50,10 +50,10 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowTableMetaDataStatement sqlStatement, final ShardingSphereMetaData metaData) {
String defaultSchema = new DatabaseTypeRegistry(currentDatabase.getProtocolType()).getDefaultSchemaName(currentDatabase.getName());
ShardingSphereSchema schema = currentDatabase.getSchema(defaultSchema);
String defaultSchema = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName());
ShardingSphereSchema schema = database.getSchema(defaultSchema);
return sqlStatement.getTableNames().stream().filter(each -> schema.getAllTableNames().contains(each.toLowerCase()))
.map(each -> buildTableRows(currentDatabase.getName(), schema, each.toLowerCase())).flatMap(Collection::stream).collect(Collectors.toList());
.map(each -> buildTableRows(database.getName(), schema, each.toLowerCase())).flatMap(Collection::stream).collect(Collectors.toList());
}

private Collection<LocalDataQueryResultRow> buildTableRows(final String databaseName, final ShardingSphereSchema schema, final String tableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.type.rdl.resource.aware.DatabaseAwareResourceDefinitionExecutor;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidateHandler;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
Expand Down Expand Up @@ -54,7 +54,7 @@
@Setter
public final class AlterStorageUnitExecutor implements DatabaseAwareResourceDefinitionExecutor<AlterStorageUnitStatement> {

private final DataSourcePoolPropertiesValidateHandler validateHandler = new DataSourcePoolPropertiesValidateHandler();
private final DataSourcePoolPropertiesValidator validateHandler = new DataSourcePoolPropertiesValidator();

private ShardingSphereDatabase database;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.distsql.handler.exception.storageunit.DuplicateStorageUnitException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.type.rdl.resource.aware.DatabaseAwareResourceDefinitionExecutor;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidateHandler;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter;
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
Expand All @@ -48,7 +48,7 @@
@Slf4j
public final class RegisterStorageUnitExecutor implements DatabaseAwareResourceDefinitionExecutor<RegisterStorageUnitStatement> {

private final DataSourcePoolPropertiesValidateHandler validateHandler = new DataSourcePoolPropertiesValidateHandler();
private final DataSourcePoolPropertiesValidator validateHandler = new DataSourcePoolPropertiesValidator();

private ShardingSphereDatabase database;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.broadcast.yaml.swapper.YamlBroadcastRuleConfigurationSwapper;
import org.apache.shardingsphere.distsql.handler.exception.datasource.MissingRequiredDataSourcesException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidateHandler;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
Expand Down Expand Up @@ -106,7 +106,7 @@ public final class YamlDatabaseConfigurationImportExecutor {

private final YamlProxyDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlProxyDataSourceConfigurationSwapper();

private final DataSourcePoolPropertiesValidateHandler validateHandler = new DataSourcePoolPropertiesValidateHandler();
private final DataSourcePoolPropertiesValidator validateHandler = new DataSourcePoolPropertiesValidator();

/**
* Import proxy database from yaml configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void assertExecute() {
when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits);
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createShardingRuleConfiguration()));
ExportDatabaseConfigurationExecutor executor = new ExportDatabaseConfigurationExecutor();
executor.setCurrentDatabase(database);
executor.setDatabase(database);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new ExportDatabaseConfigurationStatement(mock(DatabaseSegment.class), null), mock(ShardingSphereMetaData.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
Expand All @@ -101,7 +101,7 @@ void assertExecuteWithEmptyDatabase() {
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList());
ExportDatabaseConfigurationStatement sqlStatement = new ExportDatabaseConfigurationStatement(new DatabaseSegment(0, 0, new IdentifierValue("empty_db")), null);
ExportDatabaseConfigurationExecutor executor = new ExportDatabaseConfigurationExecutor();
executor.setCurrentDatabase(database);
executor.setDatabase(database);
Collection<LocalDataQueryResultRow> actual = executor.getRows(sqlStatement, mock(ShardingSphereMetaData.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ShowTableMetaDataExecutorTest {
void assertExecute() {
ShardingSphereDatabase database = mockDatabase();
ShowTableMetaDataExecutor executor = new ShowTableMetaDataExecutor();
executor.setCurrentDatabase(database);
executor.setDatabase(database);
Collection<LocalDataQueryResultRow> actual = executor.getRows(createSqlStatement(), mock(ShardingSphereMetaData.class));
assertThat(actual.size(), is(2));
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
Expand Down
Loading

0 comments on commit ed3ac53

Please sign in to comment.