This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 64d624f Move Metadata related event to SchemaRuleRegistryService
(#10445)
64d624f is described below
commit 64d624f095593c67432ceda60995c87ccb13d506
Author: Liang Zhang <[email protected]>
AuthorDate: Mon May 24 21:24:11 2021 +0800
Move Metadata related event to SchemaRuleRegistryService (#10445)
---
.../governance/core/registry/RegistryCenter.java | 45 ------------------
.../service/schema/SchemaRegistryService.java | 47 +++++++++++++++++++
.../core/registry/RegistryCenterTest.java | 47 -------------------
.../service/schema/SchemaRegistryServiceTest.java | 53 +++++++++++++++++++---
4 files changed, 94 insertions(+), 98 deletions(-)
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
index b570628..079f3aa 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.governance.core.registry;
-import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
@@ -30,8 +29,6 @@ import
org.apache.shardingsphere.governance.core.registry.listener.event.invocat
import
org.apache.shardingsphere.governance.core.registry.listener.event.invocation.ExecuteProcessUnitReportEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.invocation.ShowProcessListRequestEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.invocation.ShowProcessListResponseEvent;
-import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
-import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationCachedEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.SwitchRuleConfigurationEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.scaling.StartScalingEvent;
@@ -53,7 +50,6 @@ import
org.apache.shardingsphere.infra.executor.sql.process.model.yaml.YamlExecu
import
org.apache.shardingsphere.infra.executor.sql.process.model.yaml.YamlExecuteProcessUnit;
import
org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.CreateUserStatementEvent;
import
org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.GrantStatementEvent;
-import
org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUsers;
import
org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
@@ -62,7 +58,6 @@ import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
import java.util.ArrayList;
import java.util.Collection;
-import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -164,46 +159,6 @@ public final class RegistryCenter {
}
/**
- * Persist meta data.
- *
- * @param event meta data created event
- */
- @Subscribe
- public void renew(final MetaDataCreatedEvent event) {
- String schemaNames = repository.get(node.getMetadataNodePath());
- Collection<String> schemas = Strings.isNullOrEmpty(schemaNames) ? new
LinkedHashSet<>() : new
LinkedHashSet<>(Splitter.on(",").splitToList(schemaNames));
- if (!schemas.contains(event.getSchemaName())) {
- schemas.add(event.getSchemaName());
- repository.persist(node.getMetadataNodePath(),
Joiner.on(",").join(schemas));
- }
- }
-
- /**
- * Delete meta data.
- *
- * @param event meta data dropped event
- */
- @Subscribe
- public void renew(final MetaDataDroppedEvent event) {
- String schemaNames = repository.get(node.getMetadataNodePath());
- Collection<String> schemas = Strings.isNullOrEmpty(schemaNames) ? new
LinkedHashSet<>() : new
LinkedHashSet<>(Splitter.on(",").splitToList(schemaNames));
- if (schemas.contains(event.getSchemaName())) {
- schemas.remove(event.getSchemaName());
- repository.persist(node.getMetadataNodePath(),
Joiner.on(",").join(schemas));
- }
- }
-
- /**
- * Persist schema.
- *
- * @param event schema altered event
- */
- @Subscribe
- public void renew(final SchemaAlteredEvent event) {
- schemaService.persist(event.getSchemaName(), event.getSchema());
- }
-
- /**
* Switch rule configuration.
*
* @param event switch rule configuration event
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryService.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryService.java
index 3da5edd..9a78966 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryService.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryService.java
@@ -17,16 +17,23 @@
package org.apache.shardingsphere.governance.core.registry.service.schema;
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.governance.core.registry.RegistryCenterNode;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
import org.apache.shardingsphere.governance.core.yaml.schema.pojo.YamlSchema;
import
org.apache.shardingsphere.governance.core.yaml.schema.swapper.SchemaYamlSwapper;
import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import java.util.Collection;
+import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Optional;
@@ -79,4 +86,44 @@ public final class SchemaRegistryService {
String schemaNames = repository.get(node.getMetadataNodePath());
return Strings.isNullOrEmpty(schemaNames) ? new LinkedList<>() :
node.splitSchemaName(schemaNames);
}
+
+ /**
+ * Update when meta data created.
+ *
+ * @param event meta data created event
+ */
+ @Subscribe
+ public void update(final MetaDataCreatedEvent event) {
+ String schemaNames = repository.get(node.getMetadataNodePath());
+ Collection<String> schemas = Strings.isNullOrEmpty(schemaNames) ? new
LinkedHashSet<>() : new
LinkedHashSet<>(Splitter.on(",").splitToList(schemaNames));
+ if (!schemas.contains(event.getSchemaName())) {
+ schemas.add(event.getSchemaName());
+ repository.persist(node.getMetadataNodePath(),
Joiner.on(",").join(schemas));
+ }
+ }
+
+ /**
+ * Update when meta data altered.
+ *
+ * @param event schema altered event
+ */
+ @Subscribe
+ public void update(final SchemaAlteredEvent event) {
+ persist(event.getSchemaName(), event.getSchema());
+ }
+
+ /**
+ * Update when meta data dropped.
+ *
+ * @param event meta data dropped event
+ */
+ @Subscribe
+ public void update(final MetaDataDroppedEvent event) {
+ String schemaNames = repository.get(node.getMetadataNodePath());
+ Collection<String> schemas = Strings.isNullOrEmpty(schemaNames) ? new
LinkedHashSet<>() : new
LinkedHashSet<>(Splitter.on(",").splitToList(schemaNames));
+ if (schemas.contains(event.getSchemaName())) {
+ schemas.remove(event.getSchemaName());
+ repository.persist(node.getMetadataNodePath(),
Joiner.on(",").join(schemas));
+ }
+ }
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
index c1df1a7..9e52138 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
@@ -18,20 +18,15 @@
package org.apache.shardingsphere.governance.core.registry;
import lombok.SneakyThrows;
-import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
-import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.SwitchRuleConfigurationEvent;
import
org.apache.shardingsphere.governance.core.registry.service.config.impl.DataSourceRegistryService;
import
org.apache.shardingsphere.governance.core.registry.service.config.impl.GlobalRuleRegistryService;
import
org.apache.shardingsphere.governance.core.registry.service.config.impl.PropertiesRegistryService;
import
org.apache.shardingsphere.governance.core.registry.service.config.impl.SchemaRuleRegistryService;
-import org.apache.shardingsphere.governance.core.yaml.schema.pojo.YamlSchema;
-import
org.apache.shardingsphere.governance.core.yaml.schema.swapper.SchemaYamlSwapper;
import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
-import
org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.junit.Before;
@@ -55,7 +50,6 @@ import java.util.stream.Collectors;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -66,8 +60,6 @@ public final class RegistryCenterTest {
private static final String GLOBAL_RULE_YAML =
"yaml/regcenter/data-global-rule.yaml";
- private static final String META_DATA_YAML = "yaml/schema.yaml";
-
@Mock
private RegistryCenterRepository registryCenterRepository;
@@ -175,45 +167,6 @@ public final class RegistryCenterTest {
}
@Test
- public void assertRenewSchemaNameEventWithDrop() {
- MetaDataDroppedEvent event = new MetaDataDroppedEvent("sharding_db");
-
when(registryCenterRepository.get("/metadata")).thenReturn("sharding_db,replica_query_db");
- registryCenter.renew(event);
- verify(registryCenterRepository).persist(eq("/metadata"),
eq("replica_query_db"));
- }
-
- @Test
- public void assertRenewSchemaNameEventWithDropAndNotExist() {
- MetaDataDroppedEvent event = new MetaDataDroppedEvent("sharding_db");
-
when(registryCenterRepository.get("/metadata")).thenReturn("replica_query_db");
- registryCenter.renew(event);
- verify(registryCenterRepository, times(0)).persist(eq("/metadata"),
eq("replica_query_db"));
- }
-
- @Test
- public void assertRenewSchemaNameEventWithAdd() {
- MetaDataCreatedEvent event = new MetaDataCreatedEvent("sharding_db");
-
when(registryCenterRepository.get("/metadata")).thenReturn("replica_query_db");
- registryCenter.renew(event);
- verify(registryCenterRepository).persist(eq("/metadata"),
eq("replica_query_db,sharding_db"));
- }
-
- @Test
- public void assertRenewSchemaNameEventWithAddAndExist() {
- MetaDataCreatedEvent event = new MetaDataCreatedEvent("sharding_db");
-
when(registryCenterRepository.get("/metadata")).thenReturn("sharding_db,replica_query_db");
- registryCenter.renew(event);
- verify(registryCenterRepository, times(0)).persist(eq("/metadata"),
eq("sharding_db,replica_query_db"));
- }
-
- @Test
- public void assertRenewSchemaAlteredEvent() {
- SchemaAlteredEvent event = new SchemaAlteredEvent("sharding_db", new
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(META_DATA_YAML),
YamlSchema.class)));
- registryCenter.renew(event);
-
verify(registryCenterRepository).persist(eq("/metadata/sharding_db/schema"),
anyString());
- }
-
- @Test
@SneakyThrows
public void assertRenewSwitchRuleConfigurationEvent() {
Field field =
RegistryCenter.class.getDeclaredField("registryCacheManager");
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryServiceTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryServiceTest.java
index 750c3b2..711e341 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryServiceTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/service/schema/SchemaRegistryServiceTest.java
@@ -18,10 +18,13 @@
package org.apache.shardingsphere.governance.core.registry.service.schema;
import lombok.SneakyThrows;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataCreatedEvent;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.metadata.MetaDataDroppedEvent;
import org.apache.shardingsphere.governance.core.yaml.schema.pojo.YamlSchema;
import
org.apache.shardingsphere.governance.core.yaml.schema.swapper.SchemaYamlSwapper;
import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.junit.Before;
import org.junit.Test;
@@ -45,14 +48,13 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class SchemaRegistryServiceTest {
- private static final String YAML_DATA = "yaml/schema.yaml";
-
@Mock
private RegistryCenterRepository registryCenterRepository;
@@ -68,7 +70,7 @@ public final class SchemaRegistryServiceTest {
@Test
public void assertPersist() {
- ShardingSphereSchema schema = new
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(YAML_DATA),
YamlSchema.class));
+ ShardingSphereSchema schema = new
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(),
YamlSchema.class));
schemaRegistryService.persist("foo_db", schema);
verify(registryCenterRepository).persist(eq("/metadata/foo_db/schema"),
anyString());
}
@@ -81,7 +83,7 @@ public final class SchemaRegistryServiceTest {
@Test
public void assertLoad() {
-
when(registryCenterRepository.get("/metadata/foo_db/schema")).thenReturn(readYAML(YAML_DATA));
+
when(registryCenterRepository.get("/metadata/foo_db/schema")).thenReturn(readYAML());
Optional<ShardingSphereSchema> schemaOptional =
schemaRegistryService.load("foo_db");
assertTrue(schemaOptional.isPresent());
Optional<ShardingSphereSchema> empty =
schemaRegistryService.load("test");
@@ -103,9 +105,48 @@ public final class SchemaRegistryServiceTest {
assertThat(actual, hasItems("bar_db"));
}
+ @Test
+ public void assertUpdateWithMetaDataCreatedEvent() {
+ MetaDataCreatedEvent event = new MetaDataCreatedEvent("bar_db");
+ when(registryCenterRepository.get("/metadata")).thenReturn("foo_db");
+ schemaRegistryService.update(event);
+ verify(registryCenterRepository).persist("/metadata", "foo_db,bar_db");
+ }
+
+ @Test
+ public void assertUpdateWithMetaDataCreatedEventAndExist() {
+ MetaDataCreatedEvent event = new MetaDataCreatedEvent("bar_db");
+
when(registryCenterRepository.get("/metadata")).thenReturn("foo_db,bar_db");
+ schemaRegistryService.update(event);
+ verify(registryCenterRepository, times(0)).persist("/metadata",
"foo_db,bar_db");
+ }
+
+ @Test
+ public void assertUpdateWithMetaDataAlteredEvent() {
+ SchemaAlteredEvent event = new SchemaAlteredEvent("foo_db", new
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(),
YamlSchema.class)));
+ schemaRegistryService.update(event);
+
verify(registryCenterRepository).persist(eq("/metadata/foo_db/schema"),
anyString());
+ }
+
+ @Test
+ public void assertUpdateWithMetaDataDroppedEvent() {
+ MetaDataDroppedEvent event = new MetaDataDroppedEvent("foo_db");
+
when(registryCenterRepository.get("/metadata")).thenReturn("foo_db,bar_db");
+ schemaRegistryService.update(event);
+ verify(registryCenterRepository).persist("/metadata", "bar_db");
+ }
+
+ @Test
+ public void assertUpdateWithMetaDataDroppedEventAndNotExist() {
+ MetaDataDroppedEvent event = new MetaDataDroppedEvent("foo_db");
+ when(registryCenterRepository.get("/metadata")).thenReturn("bar_db");
+ schemaRegistryService.update(event);
+ verify(registryCenterRepository, times(0)).persist("/metadata",
"bar_db");
+ }
+
@SneakyThrows({IOException.class, URISyntaxException.class})
- private String readYAML(final String yamlFile) {
- return
Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI()))
+ private String readYAML() {
+ return
Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema.yaml").toURI()))
.stream().filter(each -> !each.startsWith("#")).map(each ->
each + System.lineSeparator()).collect(Collectors.joining());
}
}