Skip to content

Commit

Permalink
Add GlobalRuleAwareRQLExecutor (#29796)
Browse files Browse the repository at this point in the history
* Add GlobalRuleAwareRQLExecutor

* Add GlobalRuleAwareRQLExecutor

* Add GlobalRuleAwareRQLExecutor
  • Loading branch information
terrymanu authored Jan 21, 2024
1 parent a22d089 commit fd6dd1f
Show file tree
Hide file tree
Showing 58 changed files with 218 additions and 672 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,4 @@ void assertGetRowData() {
assertThat(row.getCell(2), is("DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.encrypt.distsql.handler.fixture.DistSQLEncryptAlgorithmFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowEncryptAlgorithmImplementationsStatement> executor = new ShowEncryptAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("name"));
assertThat(iterator.next(), is("type"));
assertThat(iterator.next(), is("class_path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,4 @@ void assertGetRowData() {
assertThat(row.getCell(2), is("DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.mask.distsql.handler.fixture.DistSQLMaskAlgorithmFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowMaskAlgorithmImplementationsStatement> executor = new ShowMaskAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("name"));
assertThat(iterator.next(), is("type"));
assertThat(iterator.next(), is("class_path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,4 @@ void assertGetRowData() {
assertThat(row.getCell(2), is("DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.readwritesplitting.distsql.handler.fixture.DistSQLReadQueryLoadBalanceAlgorithmFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowReadQueryLoadBalanceAlgorithmImplementationsStatement> executor = new ShowReadQueryLoadBalanceAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("name"));
assertThat(iterator.next(), is("type"));
assertThat(iterator.next(), is("class_path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,4 @@ void assertGetRowData() {
assertThat(row.getCell(2), is("DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.shadow.distsql.fixture.DistSQLShadowAlgorithmFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowShadowAlgorithmImplementationsStatement> executor = new ShowShadowAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("name"));
assertThat(iterator.next(), is("type"));
assertThat(iterator.next(), is("class_path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,4 @@ void assertGetRowData() {
assertThat(row.getCell(2), is("FOO.DISTSQL.FIXTURE"));
assertThat(row.getCell(3), is("org.apache.shardingsphere.sharding.distsql.fixture.sharding.FooDistSQLShardingAlgorithmFixture"));
}

@Test
void assertGetColumnNames() {
QueryableRALExecutor<ShowShardingAlgorithmImplementationsStatement> executor = new ShowShardingAlgorithmImplementationsExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("name"));
assertThat(iterator.next(), is("type"));
assertThat(iterator.next(), is("class_path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,31 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;
package org.apache.shardingsphere.distsql.handler.type.rql.aware;

import org.junit.jupiter.api.Test;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

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

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ShowMigrationRuleExecutorTest {
/**
* Global rule aware RQL executor.
*
* @param <T> type of SQL statement
* @param <R> type of ShardingSphere rule
*/
public interface GlobalRuleAwareRQLExecutor<T extends RQLStatement, R extends ShardingSphereRule> extends RQLExecutor<T> {

/**
* Set ShardingSphere rule.
*
* @param rule rule
*/
void setRule(R rule);

@Test
void assertGetColumnNames() {
ShowMigrationRuleExecutor executor = new ShowMigrationRuleExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("read"));
assertThat(iterator.next(), is("write"));
assertThat(iterator.next(), is("stream_channel"));
}
/**
* Get rule class.
*
* @return rule class
*/
Class<R> getRuleClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package org.apache.shardingsphere.authority.distsql.handler;

import lombok.Setter;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.distsql.statement.ShowAuthorityRuleStatement;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.distsql.handler.type.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.handler.type.rql.aware.GlobalRuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -32,19 +32,26 @@
/**
* Show authority rule executor.
*/
public final class ShowAuthorityRuleExecutor implements QueryableRALExecutor<ShowAuthorityRuleStatement> {
@Setter
public final class ShowAuthorityRuleExecutor implements GlobalRuleAwareRQLExecutor<ShowAuthorityRuleStatement, AuthorityRule> {

private AuthorityRule rule;

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("users", "provider", "props");
}

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowAuthorityRuleStatement sqlStatement, final ShardingSphereMetaData metaData) {
AuthorityRule rule = metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
public Collection<LocalDataQueryResultRow> getRows(final ShowAuthorityRuleStatement sqlStatement) {
AuthorityRuleConfiguration ruleConfig = rule.getConfiguration();
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getUsers().stream().map(each -> each.getGrantee().toString()).collect(Collectors.joining("; ")),
ruleConfig.getPrivilegeProvider().getType(), ruleConfig.getPrivilegeProvider().getProps().isEmpty() ? "" : ruleConfig.getPrivilegeProvider().getProps()));
}

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("users", "provider", "props");
public Class<AuthorityRule> getRuleClass() {
return AuthorityRule.class;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@
import org.apache.shardingsphere.authority.distsql.statement.ShowAuthorityRuleStatement;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.junit.jupiter.api.Test;

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

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -44,9 +39,11 @@ class ShowAuthorityRuleExecutorTest {

@Test
void assertExecute() {
ShardingSphereMetaData metaData = mockMetaData();
AuthorityRule rule = mock(AuthorityRule.class);
when(rule.getConfiguration()).thenReturn(createAuthorityRuleConfiguration());
ShowAuthorityRuleExecutor executor = new ShowAuthorityRuleExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowAuthorityRuleStatement.class), metaData);
executor.setRule(rule);
Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowAuthorityRuleStatement.class));
assertThat(actual.size(), is(1));
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
LocalDataQueryResultRow row = iterator.next();
Expand All @@ -55,25 +52,8 @@ void assertExecute() {
assertThat(row.getCell(3), is(""));
}

private ShardingSphereMetaData mockMetaData() {
AuthorityRule authorityRule = mock(AuthorityRule.class);
when(authorityRule.getConfiguration()).thenReturn(createAuthorityRuleConfiguration());
return new ShardingSphereMetaData(new LinkedHashMap<>(), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), new ConfigurationProperties(new Properties()));
}

private AuthorityRuleConfiguration createAuthorityRuleConfiguration() {
ShardingSphereUser root = new ShardingSphereUser("root", "", "localhost");
return new AuthorityRuleConfiguration(Collections.singleton(root), new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.emptyMap(), null);
}

@Test
void assertGetColumnNames() {
ShowAuthorityRuleExecutor executor = new ShowAuthorityRuleExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(3));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("users"));
assertThat(iterator.next(), is("provider"));
assertThat(iterator.next(), is("props"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.apache.shardingsphere.authority.distsql.statement;

import org.apache.shardingsphere.distsql.statement.ral.QueryableRALStatement;
import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;

/**
* Show authority rule statement.
*/
public final class ShowAuthorityRuleStatement extends QueryableRALStatement {
public final class ShowAuthorityRuleStatement extends RQLStatement {
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fd6dd1f

Please sign in to comment.