This is an automated email from the ASF dual-hosted git repository.
panjuan 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 f9cb36a Use direct call to instead of event post on
DropResourceBackendHandler (#11489)
f9cb36a is described below
commit f9cb36a5eaf462f093d9179bfb7131bbc823cfff
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jul 25 12:12:40 2021 +0800
Use direct call to instead of event post on DropResourceBackendHandler
(#11489)
* Add DataSourcePersistService.remove
* Use direct call to instead of event post on DropResourceBackendHandler
* Remove useless codes
* revise javadoc
---
.../governance/core/registry/RegistryCenter.java | 2 -
.../DataSourceDroppedSQLNotificationEvent.java | 36 ---------
.../subscriber/DataSourceRegistrySubscriber.java | 54 -------------
.../DataSourceRegistrySubscriberTest.java | 91 ----------------------
.../service/impl/DataSourcePersistService.java | 21 ++++-
.../service/impl/DataSourcePersistServiceTest.java | 29 +++++--
.../rdl/resource/DropResourceBackendHandler.java | 10 +--
7 files changed, 42 insertions(+), 201 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 56b9b0e..015a2d8 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
@@ -21,7 +21,6 @@ import lombok.Getter;
import org.apache.shardingsphere.governance.core.GovernanceInstance;
import
org.apache.shardingsphere.governance.core.lock.service.LockRegistryService;
import
org.apache.shardingsphere.governance.core.registry.cache.subscriber.ScalingRegistrySubscriber;
-import
org.apache.shardingsphere.governance.core.registry.config.subscriber.DataSourceRegistrySubscriber;
import
org.apache.shardingsphere.governance.core.registry.config.subscriber.GlobalRuleRegistrySubscriber;
import
org.apache.shardingsphere.governance.core.registry.config.subscriber.SchemaRuleRegistrySubscriber;
import
org.apache.shardingsphere.governance.core.registry.metadata.subscriber.SchemaMetaDataRegistrySubscriber;
@@ -61,7 +60,6 @@ public final class RegistryCenter {
}
private void createSubscribers(final RegistryCenterRepository repository) {
- new DataSourceRegistrySubscriber(repository);
new SchemaMetaDataRegistrySubscriber(repository);
new GlobalRuleRegistrySubscriber(repository);
new SchemaRuleRegistrySubscriber(repository);
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/event/datasource/DataSourceDroppedSQLNotificationEvent.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/event/datasource/DataSourceDroppedSQLNotificationEvent.java
deleted file mode 100644
index 24a2d17..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/event/datasource/DataSourceDroppedSQLNotificationEvent.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package
org.apache.shardingsphere.governance.core.registry.config.event.datasource;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.governance.core.registry.SQLNotificationEvent;
-
-import java.util.Collection;
-
-/**
- * Data source dropped SQL notification event.
- */
-@RequiredArgsConstructor
-@Getter
-public final class DataSourceDroppedSQLNotificationEvent implements
SQLNotificationEvent {
-
- private final String schemaName;
-
- private final Collection<String> dataSourceNames;
-}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriber.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriber.java
deleted file mode 100644
index 158d559..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriber.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.governance.core.registry.config.subscriber;
-
-import com.google.common.eventbus.Subscribe;
-import
org.apache.shardingsphere.governance.core.registry.config.event.datasource.DataSourceDroppedSQLNotificationEvent;
-import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import
org.apache.shardingsphere.infra.config.persist.service.impl.DataSourcePersistService;
-import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-
-import java.util.Map;
-
-/**
- * Data source registry subscriber.
- */
-public final class DataSourceRegistrySubscriber {
-
- private final DataSourcePersistService persistService;
-
- public DataSourceRegistrySubscriber(final RegistryCenterRepository
repository) {
- persistService = new DataSourcePersistService(repository);
- ShardingSphereEventBus.getInstance().register(this);
- }
-
- /**
- * Update data source configurations for drop.
- *
- * @param event data source dropped event
- */
- @Subscribe
- public void update(final DataSourceDroppedSQLNotificationEvent event) {
- Map<String, DataSourceConfiguration> dataSourceConfigs =
persistService.load(event.getSchemaName());
- for (String each : event.getDataSourceNames()) {
- dataSourceConfigs.remove(each);
- }
- persistService.persist(event.getSchemaName(), dataSourceConfigs);
- }
-}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriberTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriberTest.java
deleted file mode 100644
index b6fcb96..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/config/subscriber/DataSourceRegistrySubscriberTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.governance.core.registry.config.subscriber;
-
-import
org.apache.shardingsphere.governance.core.registry.config.event.datasource.DataSourceDroppedSQLNotificationEvent;
-import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import
org.apache.shardingsphere.infra.config.persist.service.impl.DataSourcePersistService;
-import org.apache.shardingsphere.test.mock.MockedDataSource;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import javax.sql.DataSource;
-import java.lang.reflect.Field;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.stream.Collectors;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class DataSourceRegistrySubscriberTest {
-
- private DataSourceRegistrySubscriber dataSourceRegistrySubscriber;
-
- @Mock
- private DataSourcePersistService persistService;
-
- @Before
- public void setUp() throws ReflectiveOperationException {
- dataSourceRegistrySubscriber = new
DataSourceRegistrySubscriber(mock(RegistryCenterRepository.class));
- Field field =
dataSourceRegistrySubscriber.getClass().getDeclaredField("persistService");
- field.setAccessible(true);
- field.set(dataSourceRegistrySubscriber, persistService);
- }
-
- @Test
- public void assertUpdateWithDataSourceAlteredEvent() {
- DataSourceDroppedSQLNotificationEvent event = new
DataSourceDroppedSQLNotificationEvent("foo_db",
Collections.singletonList("ds_0"));
- Map<String, DataSourceConfiguration> dataSourceConfigs =
createDataSourceConfigurations();
- when(persistService.load("foo_db")).thenReturn(dataSourceConfigs);
- dataSourceRegistrySubscriber.update(event);
- dataSourceConfigs.remove("ds_0");
- verify(persistService).persist("foo_db", dataSourceConfigs);
- }
-
- private Map<String, DataSourceConfiguration>
createDataSourceConfigurations() {
- return
createDataSourceMap().entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry ->
-
DataSourceConfiguration.getDataSourceConfiguration(entry.getValue()),
(oldValue, currentValue) -> oldValue, LinkedHashMap::new));
- }
-
- private Map<String, DataSource> createDataSourceMap() {
- Map<String, DataSource> result = new LinkedHashMap<>(2, 1);
- result.put("ds_0", createDataSource("ds_0"));
- result.put("ds_1", createDataSource("ds_1"));
- return result;
- }
-
- private DataSource createDataSource(final String name) {
- MockedDataSource result = new MockedDataSource();
- result.setDriverClassName("com.mysql.jdbc.Driver");
- result.setUrl("jdbc:mysql://localhost:3306/" + name);
- result.setUsername("root");
- result.setPassword("root");
- result.setConnectionInitSqls(Arrays.asList("set names utf8mb4;", "set
names utf8;"));
- return result;
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistService.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistService.java
index 5e04229..3a37672 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistService.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistService.java
@@ -19,13 +19,14 @@ package
org.apache.shardingsphere.infra.config.persist.service.impl;
import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.persist.node.SchemaMetadataNode;
-import
org.apache.shardingsphere.infra.config.persist.service.SchemaBasedPersistService;
import
org.apache.shardingsphere.infra.config.persist.repository.ConfigCenterRepository;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import
org.apache.shardingsphere.infra.config.persist.service.SchemaBasedPersistService;
import
org.apache.shardingsphere.infra.yaml.config.swapper.YamlDataSourceConfigurationSwapper;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -88,4 +89,18 @@ public final class DataSourcePersistService implements
SchemaBasedPersistService
dataSourceConfigs.putAll(toBeAppendedDataSourceConfigs);
persist(schemaName, dataSourceConfigs);
}
+
+ /**
+ * Drop data sources.
+ *
+ * @param schemaName schema name
+ * @param toBeDroppedDataSourceNames data sources to be dropped
+ */
+ public void drop(final String schemaName, final Collection<String>
toBeDroppedDataSourceNames) {
+ Map<String, DataSourceConfiguration> dataSourceConfigs =
load(schemaName);
+ for (String each : toBeDroppedDataSourceNames) {
+ dataSourceConfigs.remove(each);
+ }
+ persist(schemaName, dataSourceConfigs);
+ }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistServiceTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistServiceTest.java
index 5874876..0065299 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistServiceTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/persist/service/impl/DataSourcePersistServiceTest.java
@@ -78,6 +78,28 @@ public final class DataSourcePersistServiceTest {
assertTrue(actual.isEmpty());
}
+ @Test
+ public void assertAppend() {
+ when(repository.get("/metadata/foo_db/dataSources")).thenReturn("");
+ new DataSourcePersistService(repository).append("foo_db",
Collections.singletonMap("foo_ds",
DataSourceConfiguration.getDataSourceConfiguration(createDataSource("foo_ds"))));
+ // TODO load from YAML file
+ String expected = "foo_ds:\n" + " driverClassName:
com.mysql.jdbc.Driver\n" + " password: root\n"
+ + " dataSourceClassName:
org.apache.shardingsphere.test.mock.MockedDataSource\n" + "
connectionInitSqls:\n" + " - set names utf8mb4;\n"
+ + " - set names utf8;\n" + " url:
jdbc:mysql://localhost:3306/foo_ds\n" + " username: root\n";
+ verify(repository).persist("/metadata/foo_db/dataSources", expected);
+ }
+
+ @Test
+ public void assertDrop() {
+ // TODO load from YAML file
+ String actual = "foo_ds:\n" + " driverClassName:
com.mysql.jdbc.Driver\n" + " password: root\n"
+ + " dataSourceClassName:
org.apache.shardingsphere.test.mock.MockedDataSource\n" + "
connectionInitSqls:\n" + " - set names utf8mb4;\n"
+ + " - set names utf8;\n" + " url:
jdbc:mysql://localhost:3306/foo_ds\n" + " username: root\n";
+
when(repository.get("/metadata/foo_db/dataSources")).thenReturn(actual);
+ new DataSourcePersistService(repository).drop("foo_db",
Collections.singleton("foo_ds"));
+ verify(repository).persist("/metadata/foo_db/dataSources", "{}\n");
+ }
+
private DataSource createDataSource(final String name) {
MockedDataSource result = new MockedDataSource();
result.setDriverClassName("com.mysql.jdbc.Driver");
@@ -87,11 +109,4 @@ public final class DataSourcePersistServiceTest {
result.setConnectionInitSqls(Arrays.asList("set names utf8mb4;", "set
names utf8;"));
return result;
}
-
- @Test
- public void assertAppend() {
- when(repository.get("/metadata/foo_db/dataSources")).thenReturn("");
- new DataSourcePersistService(repository).append("foo_db",
Collections.emptyMap());
- verify(repository).persist("/metadata/foo_db/dataSources", "{}\n");
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
index 7788d5e..5860180 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
@@ -18,12 +18,10 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceStatement;
-import
org.apache.shardingsphere.governance.core.registry.config.event.datasource.DataSourceDroppedSQLNotificationEvent;
import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.ResourceDefinitionViolationException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.ResourceInUsedException;
-import
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
@@ -55,7 +53,7 @@ public final class DropResourceBackendHandler extends
SchemaRequiredBackendHandl
Collection<String> toBeDroppedResourceNames = sqlStatement.getNames();
check(schemaName, toBeDroppedResourceNames);
drop(schemaName, toBeDroppedResourceNames);
- post(schemaName, toBeDroppedResourceNames);
+
ProxyContext.getInstance().getMetaDataContexts().getConfigCenter().getDataSourceService().drop(schemaName,
toBeDroppedResourceNames);
return new UpdateResponseHeader(sqlStatement);
}
@@ -113,8 +111,4 @@ public final class DropResourceBackendHandler extends
SchemaRequiredBackendHandl
ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources().remove(each);
}
}
-
- private void post(final String schemaName, final Collection<String>
toBeDroppedResourceNames) {
- ShardingSphereEventBus.getInstance().post(new
DataSourceDroppedSQLNotificationEvent(schemaName, toBeDroppedResourceNames));
- }
}