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

Refactor GlobalRulesBuilder #33893

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setUp() {

@Test
void assertReadWithoutFraction() {
int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue();
int currentSeconds = (int) (System.currentTimeMillis() / 1000L);
when(byteBuf.readInt()).thenReturn(currentSeconds);
assertThat(new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L)));
}
Expand All @@ -61,8 +61,8 @@ void assertReadWithoutFraction() {
void assertReadWithFraction() {
columnDef.setColumnMeta(1);
long currentTimeMillis = 1678795614082L;
int currentSeconds = Long.valueOf(currentTimeMillis / 1000L).intValue();
int currentMilliseconds = Long.valueOf(currentTimeMillis % 100L).intValue();
int currentSeconds = (int) (System.currentTimeMillis() / 1000L);
int currentMilliseconds = (int) (currentTimeMillis % 100L);
when(payload.readInt1()).thenReturn(currentMilliseconds);
when(byteBuf.readInt()).thenReturn(currentSeconds);
assertThat("currentTimeMillis:" + currentTimeMillis, new MySQLTimestamp2BinlogProtocolValue().read(columnDef, payload), is(new Timestamp(currentSeconds * 1000L + currentMilliseconds * 10L)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MySQLTimestampBinlogProtocolValueTest {

@Test
void assertRead() {
int currentSeconds = Long.valueOf(System.currentTimeMillis() / 1000L).intValue();
int currentSeconds = (int) (System.currentTimeMillis() / 1000L);
when(payload.readInt4()).thenReturn(currentSeconds);
assertThat(new MySQLTimestampBinlogProtocolValue().read(columnDef, payload),
is(DateTimeFormatterFactory.getStandardFormatter().format(new Timestamp(currentSeconds * 1000L).toLocalDateTime())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,23 @@ void assertWriteIntLenencWithOneByte() {

@Test
void assertWriteIntLenencWithTwoBytes() {
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 16D)).longValue() - 1L);
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 16D)) - 1L);
verify(byteBuf).writeByte(0xfc);
verify(byteBuf).writeShortLE(Double.valueOf(Math.pow(2D, 16D)).intValue() - 1);
verify(byteBuf).writeShortLE((int) (Math.pow(2D, 16D)) - 1);
}

@Test
void assertWriteIntLenencWithThreeBytes() {
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 24D)).longValue() - 1L);
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 24D)) - 1L);
verify(byteBuf).writeByte(0xfd);
verify(byteBuf).writeMediumLE(Double.valueOf(Math.pow(2D, 24D)).intValue() - 1);
verify(byteBuf).writeMediumLE((int) (Math.pow(2D, 24D)) - 1);
}

@Test
void assertWriteIntLenencWithFourBytes() {
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc(Double.valueOf(Math.pow(2D, 25D)).longValue() - 1L);
new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8).writeIntLenenc((long) (Math.pow(2D, 25D)) - 1L);
verify(byteBuf).writeByte(0xfe);
verify(byteBuf).writeLongLE(Double.valueOf(Math.pow(2D, 25D)).intValue() - 1L);
verify(byteBuf).writeLongLE((int) (Math.pow(2D, 25D)) - 1L);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public final class GlobalRulesBuilder {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Collection<ShardingSphereRule> buildRules(final Collection<RuleConfiguration> globalRuleConfigs,
final Map<String, ShardingSphereDatabase> databases, final ConfigurationProperties props) {
final Collection<ShardingSphereDatabase> databases, final ConfigurationProperties props) {
Collection<ShardingSphereRule> result = new LinkedList<>();
for (Entry<RuleConfiguration, GlobalRuleBuilder> entry : getRuleBuilderMap(globalRuleConfigs).entrySet()) {
result.add(entry.getValue().build(entry.getKey(), databases.values(), props));
result.add(entry.getValue().build(entry.getKey(), databases, props));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GlobalRulesBuilderTest {
@Test
void assertBuildRules() {
Collection<ShardingSphereRule> shardingSphereRules = GlobalRulesBuilder.buildRules(
Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singletonMap("foo_db", buildDatabase()), mock(ConfigurationProperties.class));
Collections.singletonList(new FixtureGlobalRuleConfiguration()), Collections.singleton(buildDatabase()), mock(ConfigurationProperties.class));
assertThat(shardingSphereRules.size(), is(1));
assertThat(shardingSphereRules.iterator().next(), instanceOf(FixtureGlobalRule.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ void assertRollbackWithoutBegin() {
}

private void assertResult(final Class<?> requestClass, final Class<?> responseClass) {
assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterTMRequest));
assertTrue(requestQueue.stream().anyMatch(each -> each instanceof RegisterRMRequest));
assertTrue(requestQueue.stream().anyMatch(RegisterTMRequest.class::isInstance));
assertTrue(requestQueue.stream().anyMatch(RegisterRMRequest.class::isInstance));
assertTrue(requestQueue.stream().anyMatch(each -> requestClass.equals(each.getClass())));
assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterTMResponse));
assertTrue(responseQueue.stream().anyMatch(each -> each instanceof RegisterRMResponse));
assertTrue(responseQueue.stream().anyMatch(RegisterTMResponse.class::isInstance));
assertTrue(responseQueue.stream().anyMatch(RegisterRMResponse.class::isInstance));
assertTrue(responseQueue.stream().anyMatch(each -> responseClass.equals(each.getClass())));
while (!requestQueue.isEmpty()) {
Object requestPackage = requestQueue.poll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat
.createChangedDatabases(database.getName(), false, switchingResource, ruleConfigs, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = new ConfigurationProperties(metaDataPersistService.getPropsService().load());
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases, props));
GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases.values(), props));
MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService,
new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
switchingResource.closeStaleDataSources();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService
final ConfigurationProperties props) {
// TODO load global data sources from persist service
ResourceMetaData globalResourceMetaData = new ResourceMetaData(param.getGlobalDataSources());
RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases, props));
RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases.values(), props));
ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props);
ShardingSphereStatistics shardingSphereStatistics = initStatistics(persistService, shardingSphereMetaData);
return new MetaDataContexts(shardingSphereMetaData, shardingSphereStatistics);
Expand Down Expand Up @@ -219,7 +219,7 @@ private static void restoreRules(final MetaDataContexts metaDataContexts, final
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().removeIf(eachRule -> each.getRuleType() == eachRule.getClass());
RuleConfiguration restoredRuleConfig = each.restore(rule.getConfiguration());
ShardingSphereRule rebuiltRule = GlobalRulesBuilder.buildRules(
Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
Collections.singleton(restoredRuleConfig), metaDataContexts.getMetaData().getAllDatabases(), metaDataContexts.getMetaData().getProps()).iterator().next();
metaDataContexts.getMetaData().getGlobalRuleMetaData().getRules().add(rebuiltRule);
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public static MetaDataContexts createBySwitchResource(final String databaseName,
createChangedDatabases(databaseName, internalLoadMetaData, switchingResource, null, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props));
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props));
return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
}

Expand All @@ -294,7 +294,7 @@ public static MetaDataContexts createByAlterRule(final String databaseName, fina
createChangedDatabases(databaseName, internalLoadMetaData, null, ruleConfigs, originalMetaDataContexts, metaDataPersistService, computeNodeInstanceContext);
ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps();
RuleMetaData changedGlobalMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases, props));
GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props));
return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void setUp() throws SQLException {
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
when(ExternalMetaDataFactory.create(anyMap(), any(), any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database)));
when(GlobalRulesBuilder.buildRules(anyCollection(), anyMap(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule()));
when(GlobalRulesBuilder.buildRules(anyCollection(), anyCollection(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule()));
}

private DatabaseRulePersistService mockDatabaseRulePersistService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ private void assertShardingRuleConfiguration(final YamlProxyDatabaseConfiguratio
assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/ds_0");
assertDataSourceConfiguration(actual.getDataSources().get("ds_1"), "jdbc:mysql://127.0.0.1:3306/ds_1");
Optional<YamlShardingRuleConfiguration> shardingRuleConfig = actual.getRules().stream()
.filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each);
.filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast);
assertTrue(shardingRuleConfig.isPresent());
assertShardingRuleConfiguration(shardingRuleConfig.get());
assertFalse(
actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent());
actual.getRules().stream().filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast).isPresent());
}

private void assertShardingRuleConfiguration(final YamlShardingRuleConfiguration actual) {
Expand All @@ -96,11 +96,11 @@ private void assertReadwriteSplittingRuleConfiguration(final YamlProxyDatabaseCo
assertDataSourceConfiguration(actual.getDataSources().get("write_ds"), "jdbc:mysql://127.0.0.1:3306/write_ds");
assertDataSourceConfiguration(actual.getDataSources().get("read_ds_0"), "jdbc:mysql://127.0.0.1:3306/read_ds_0");
assertDataSourceConfiguration(actual.getDataSources().get("read_ds_1"), "jdbc:mysql://127.0.0.1:3306/read_ds_1");
assertFalse(actual.getRules().stream().filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent());
assertFalse(actual.getRules().stream().filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent());
assertFalse(
actual.getRules().stream().filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each).isPresent());
Optional<YamlReadwriteSplittingRuleConfiguration> ruleConfig = actual.getRules().stream()
.filter(each -> each instanceof YamlReadwriteSplittingRuleConfiguration).findFirst().map(each -> (YamlReadwriteSplittingRuleConfiguration) each);
.filter(YamlReadwriteSplittingRuleConfiguration.class::isInstance).findFirst().map(YamlReadwriteSplittingRuleConfiguration.class::cast);
assertTrue(ruleConfig.isPresent());
for (YamlReadwriteSplittingDataSourceGroupRuleConfiguration each : ruleConfig.get().getDataSourceGroups().values()) {
assertReadwriteSplittingRuleConfiguration(each);
Expand All @@ -117,9 +117,9 @@ private void assertEncryptRuleConfiguration(final YamlProxyDatabaseConfiguration
assertThat(actual.getDataSources().size(), is(1));
assertDataSourceConfiguration(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/encrypt_ds");
assertFalse(actual.getRules().stream()
.filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(each -> (YamlShardingRuleConfiguration) each).isPresent());
.filter(YamlShardingRuleConfiguration.class::isInstance).findFirst().map(YamlShardingRuleConfiguration.class::cast).isPresent());
Optional<YamlEncryptRuleConfiguration> encryptRuleConfig = actual.getRules().stream()
.filter(each -> each instanceof YamlEncryptRuleConfiguration).findFirst().map(each -> (YamlEncryptRuleConfiguration) each);
.filter(YamlEncryptRuleConfiguration.class::isInstance).findFirst().map(YamlEncryptRuleConfiguration.class::cast);
assertTrue(encryptRuleConfig.isPresent());
assertEncryptRuleConfiguration(encryptRuleConfig.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void assertAuthorityRuleConfiguration(final ProxyConfiguration proxyConf
}

private Optional<AuthorityRuleConfiguration> findAuthorityRuleConfiguration(final Collection<RuleConfiguration> globalRuleConfigs) {
return globalRuleConfigs.stream().filter(each -> each instanceof AuthorityRuleConfiguration).findFirst().map(each -> (AuthorityRuleConfiguration) each);
return globalRuleConfigs.stream().filter(AuthorityRuleConfiguration.class::isInstance).findFirst().map(AuthorityRuleConfiguration.class::cast);
}

private void assertProxyConfigurationProps(final ProxyConfiguration proxyConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void start() {
}

private void startDependencies() {
Collection<DockerITContainer> dependencies = getDependencies().stream().filter(each -> each instanceof DockerITContainer).map(each -> (DockerITContainer) each).collect(Collectors.toList());
Collection<DockerITContainer> dependencies = getDependencies().stream().filter(DockerITContainer.class::isInstance).map(DockerITContainer.class::cast).collect(Collectors.toList());
dependencies.stream().filter(each -> !each.isCreated()).forEach(GenericContainer::start);
dependencies.stream()
.filter(each -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void start() {

@Override
public Collection<ITContainer> getContainers() {
return proxyClusterContainers.stream().map(each -> (ITContainer) each).collect(Collectors.toList());
return proxyClusterContainers.stream().map(ITContainer.class::cast).collect(Collectors.toList());
}

private static class RandomDataSourceAdapter implements DataSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected Collection<ShardingSphereSchema> mockSchemas(final String schemaName)

@Override
protected void mockRules(final Collection<ShardingSphereRule> rules, final String schemaName, final SQLStatement sqlStatement) {
Optional<SingleRule> singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst();
Optional<SingleRule> singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst();
if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) {
singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account");
singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("encrypt_ds", schemaName, "t_account_bak");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected YamlRootConfiguration createRootConfiguration(final SQLRewriteEngineTe

@Override
protected void mockRules(final Collection<ShardingSphereRule> rules, final String schemaName, final SQLStatement sqlStatement) {
Optional<SingleRule> singleRule = rules.stream().filter(each -> each instanceof SingleRule).map(each -> (SingleRule) each).findFirst();
Optional<SingleRule> singleRule = rules.stream().filter(SingleRule.class::isInstance).map(SingleRule.class::cast).findFirst();
if (singleRule.isPresent() && !(sqlStatement instanceof CreateTableStatement)) {
singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single");
singleRule.get().getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).put("db", schemaName, "t_single_extend");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private Connection openConnection() throws SQLException {

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
private DataSource createDataSource() throws SQLException {
Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(e -> e instanceof CommunicationsException).until(() -> {
Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptionsMatching(CommunicationsException.class::isInstance).until(() -> {
openConnection().close();
return true;
});
Expand Down
Loading
Loading