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 57388a1 Use direct call to instead of event post on
DropDatabaseBackendHandler (#11494)
57388a1 is described below
commit 57388a1e456ba1c5ea1114fb5f53c49e38aa562c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jul 25 15:25:18 2021 +0800
Use direct call to instead of event post on DropDatabaseBackendHandler
(#11494)
* Use direct call to instead of event post on DropDatabaseBackendHandler
* Fix test case
---
.../event/DatabaseDroppedSQLNotificationEvent.java | 32 --------------------
.../SchemaMetaDataRegistrySubscriber.java | 11 -------
.../SchemaMetaDataRegistrySubscriberTest.java | 8 -----
.../text/database/DropDatabaseBackendHandler.java | 9 +-----
.../DatabaseOperateBackendHandlerFactoryTest.java | 34 ++++++++++------------
.../database/DropDatabaseBackendHandlerTest.java | 3 +-
6 files changed, 18 insertions(+), 79 deletions(-)
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/event/DatabaseDroppedSQLNotificationEvent.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/event/DatabaseDroppedSQLNotificationEvent.java
deleted file mode 100644
index 554af26..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/event/DatabaseDroppedSQLNotificationEvent.java
+++ /dev/null
@@ -1,32 +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.metadata.event;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.governance.core.registry.SQLNotificationEvent;
-
-/**
- * Database dropped SQL notificationEvent event.
- */
-@RequiredArgsConstructor
-@Getter
-public final class DatabaseDroppedSQLNotificationEvent implements
SQLNotificationEvent {
-
- private final String databaseName;
-}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriber.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriber.java
index 1e54fd8..3408afd 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriber.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriber.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.governance.core.registry.metadata.subscriber;
import com.google.common.eventbus.Subscribe;
-import
org.apache.shardingsphere.governance.core.registry.metadata.event.DatabaseDroppedSQLNotificationEvent;
import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
import
org.apache.shardingsphere.infra.config.persist.service.SchemaMetaDataPersistService;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
@@ -45,14 +44,4 @@ public final class SchemaMetaDataRegistrySubscriber {
public void update(final SchemaAlteredEvent event) {
persistService.persist(event.getSchemaName(), event.getSchema());
}
-
- /**
- * Update when database dropped.
- *
- * @param event database dropped SQL notification event
- */
- @Subscribe
- public void update(final DatabaseDroppedSQLNotificationEvent event) {
- persistService.delete(event.getDatabaseName());
- }
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriberTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriberTest.java
index 5da7fdf..fc509d6 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriberTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/metadata/subscriber/SchemaMetaDataRegistrySubscriberTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.governance.core.registry.metadata.subscriber;
-import
org.apache.shardingsphere.governance.core.registry.metadata.event.DatabaseDroppedSQLNotificationEvent;
import
org.apache.shardingsphere.governance.repository.spi.RegistryCenterRepository;
import
org.apache.shardingsphere.infra.config.persist.service.SchemaMetaDataPersistService;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
@@ -55,11 +54,4 @@ public final class SchemaMetaDataRegistrySubscriberTest {
schemaMetaDataRegistrySubscriber.update(event);
verify(persistService).persist("foo_db", event.getSchema());
}
-
- @Test
- public void assertUpdateWithDatabaseDroppedSQLNotificationEvent() {
- DatabaseDroppedSQLNotificationEvent event = new
DatabaseDroppedSQLNotificationEvent("foo_db");
- schemaMetaDataRegistrySubscriber.update(event);
- verify(persistService).delete("foo_db");
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandler.java
index 65db508..839e701 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandler.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.proxy.backend.text.database;
import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.governance.core.registry.metadata.event.DatabaseDroppedSQLNotificationEvent;
-import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
@@ -45,7 +43,7 @@ public final class DropDatabaseBackendHandler implements
TextProtocolBackendHand
if (isDropCurrentDatabase(sqlStatement.getDatabaseName())) {
backendConnection.setCurrentSchema(null);
}
- post(sqlStatement);
+
ProxyContext.getInstance().getMetaDataContexts().getConfigCenter().getSchemaMetaDataService().delete(sqlStatement.getDatabaseName());
return new UpdateResponseHeader(sqlStatement);
}
@@ -58,9 +56,4 @@ public final class DropDatabaseBackendHandler implements
TextProtocolBackendHand
private boolean isDropCurrentDatabase(final String databaseName) {
return !Strings.isNullOrEmpty(backendConnection.getSchemaName()) &&
backendConnection.getSchemaName().equals(databaseName);
}
-
- private void post(final DropDatabaseStatement sqlStatement) {
- // TODO Need to get the executed feedback from registry center for
returning.
- ShardingSphereEventBus.getInstance().post(new
DatabaseDroppedSQLNotificationEvent(sqlStatement.getDatabaseName()));
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseOperateBackendHandlerFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseOperateBackendHandlerFactoryTest.java
index 27a4e4f..cfa353d 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseOperateBackendHandlerFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseOperateBackendHandlerFactoryTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.backend.text.admin;
-import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.persist.ConfigCenter;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -39,6 +38,7 @@ import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQ
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLDropDatabaseStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLCreateDatabaseStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLDropDatabaseStatement;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -64,8 +64,9 @@ public final class DatabaseOperateBackendHandlerFactoryTest {
public void setUp() throws IllegalAccessException, NoSuchFieldException {
Field metaDataContexts =
ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
metaDataContexts.setAccessible(true);
- metaDataContexts.set(ProxyContext.getInstance(), new
StandardMetaDataContexts(mock(ConfigCenter.class), getMetaDataMap(),
- mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ConfigurationProperties(new Properties()),
mock(OptimizeContextFactory.class)));
+ ProxyContext.getInstance().init(new
StandardMetaDataContexts(mock(ConfigCenter.class),
+ getMetaDataMap(), mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ConfigurationProperties(new Properties()),
mock(OptimizeContextFactory.class)),
+ mock(TransactionContexts.class));
}
@Test
@@ -77,7 +78,7 @@ public final class DatabaseOperateBackendHandlerFactoryTest {
public void assertExecutePostgreSQLCreateDatabaseContext() throws
SQLException {
assertExecuteCreateDatabaseContext(new
PostgreSQLCreateDatabaseStatement());
}
-
+
private void assertExecuteCreateDatabaseContext(final
CreateDatabaseStatement sqlStatement) throws SQLException {
BackendConnection connection = mock(BackendConnection.class);
when(connection.getSchemaName()).thenReturn("schema");
@@ -149,22 +150,17 @@ public final class
DatabaseOperateBackendHandlerFactoryTest {
return Collections.singletonMap("schema", metaData);
}
- @SneakyThrows(ReflectiveOperationException.class)
private void setGovernanceMetaDataContexts(final boolean isGovernance) {
- Field metaDataContexts =
ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
- metaDataContexts.setAccessible(true);
- if (isGovernance) {
- MetaDataContexts mockedMetaDataContexts =
mock(MetaDataContexts.class);
-
when(mockedMetaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("schema"));
- ShardingSphereMetaData metaData =
mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
- when(metaData.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
-
when(metaData.getResource().getDataSources()).thenReturn(Collections.emptyMap());
-
when(metaData.getResource().getNotExistedResources(any())).thenReturn(Collections.emptyList());
-
when(mockedMetaDataContexts.getMetaData("schema")).thenReturn(metaData);
- metaDataContexts.set(ProxyContext.getInstance(),
mockedMetaDataContexts);
- } else {
- metaDataContexts.set(ProxyContext.getInstance(), new
StandardMetaDataContexts(mock(ConfigCenter.class)));
- }
+ ProxyContext.getInstance().init(isGovernance ? mockMetaDataContexts()
: new StandardMetaDataContexts(mock(ConfigCenter.class)),
mock(TransactionContexts.class));
+ }
+
+ private MetaDataContexts mockMetaDataContexts() {
+ MetaDataContexts result = mock(MetaDataContexts.class,
RETURNS_DEEP_STUBS);
+
when(result.getAllSchemaNames()).thenReturn(Collections.singletonList("schema"));
+
when(result.getMetaData("schema").getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+
when(result.getMetaData("schema").getResource().getDataSources()).thenReturn(Collections.emptyMap());
+
when(result.getMetaData("schema").getResource().getNotExistedResources(any())).thenReturn(Collections.emptyList());
+ return result;
}
@After
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandlerTest.java
index 313d491..337a853 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/database/DropDatabaseBackendHandlerTest.java
@@ -28,6 +28,7 @@ import
org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@@ -51,7 +52,7 @@ public final class DropDatabaseBackendHandlerTest {
@Mock
private TransactionContexts transactionContexts;
- @Mock
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private MetaDataContexts metaDataContexts;
private DropDatabaseBackendHandler handler;