Skip to content

Commit

Permalink
Remove useless ListenerAssisted (#34196)
Browse files Browse the repository at this point in the history
* Remove useless ListenerAssisted

* Remove useless ListenerAssisted

* Remove useless ListenerAssisted

* Remove useless ListenerAssisted
  • Loading branch information
terrymanu authored Dec 29, 2024
1 parent 7bee9ad commit c627064
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.mode.persist.pojo;
package org.apache.shardingsphere.mode.persist;

/**
* Listener assisted type.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
package org.apache.shardingsphere.mode.persist.service;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.metadata.persist.node.StatesNode;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
import org.apache.shardingsphere.mode.persist.ListenerAssistedType;
import org.apache.shardingsphere.mode.spi.PersistRepository;

/**
Expand All @@ -34,10 +33,11 @@ public final class ListenerAssistedPersistService {
/**
* Persist database name listener assisted.
*
* @param listenerAssisted listener assisted pojo
* @param databaseName database name
* @param listenerAssistedType listener assisted type
*/
public void persistDatabaseNameListenerAssisted(final ListenerAssisted listenerAssisted) {
repository.persistEphemeral(StatesNode.getDatabaseNameListenerAssistedNodePath(listenerAssisted.getDatabaseName()), YamlEngine.marshal(listenerAssisted));
public void persistDatabaseNameListenerAssisted(final String databaseName, final ListenerAssistedType listenerAssistedType) {
repository.persistEphemeral(StatesNode.getDatabaseNameListenerAssistedNodePath(databaseName), listenerAssistedType.name());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

package org.apache.shardingsphere.mode.persist.service;

import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssistedType;
import org.apache.shardingsphere.mode.persist.ListenerAssistedType;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -36,8 +35,8 @@ class ListenerAssistedPersistServiceTest {

@Test
void assertPersistDatabaseNameListenerAssisted() {
new ListenerAssistedPersistService(repository).persistDatabaseNameListenerAssisted(new ListenerAssisted("foo_db", ListenerAssistedType.CREATE_DATABASE));
verify(repository).persistEphemeral("/states/listener_assisted/foo_db", "databaseName: foo_db" + System.lineSeparator() + "listenerAssistedType: CREATE_DATABASE" + System.lineSeparator());
new ListenerAssistedPersistService(repository).persistDatabaseNameListenerAssisted("foo_db", ListenerAssistedType.CREATE_DATABASE);
verify(repository).persistEphemeral("/states/listener_assisted/foo_db", ListenerAssistedType.CREATE_DATABASE.name());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type;

import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.metadata.persist.node.StatesNode;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssistedType;
import org.apache.shardingsphere.mode.persist.ListenerAssistedType;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -49,13 +47,12 @@ public Collection<DataChangedEvent.Type> getSubscribedTypes() {

@Override
public Optional<DispatchEvent> build(final DataChangedEvent event) {
Optional<String> databaseName = StatesNode.getDatabaseNameByListenerAssistedNodePath(event.getKey());
if (!databaseName.isPresent()) {
return Optional.empty();
}
ListenerAssistedType listenerAssistedType = YamlEngine.unmarshal(event.getValue(), ListenerAssisted.class).getListenerAssistedType();
return Optional.of(ListenerAssistedType.CREATE_DATABASE == listenerAssistedType
? new CreateDatabaseListenerAssistedEvent(databaseName.get())
: new DropDatabaseListenerAssistedEvent(databaseName.get()));
return StatesNode.getDatabaseNameByListenerAssistedNodePath(event.getKey()).map(optional -> createDatabaseListenerAssistedEvent(optional, event));
}

private DispatchEvent createDatabaseListenerAssistedEvent(final String databaseName, final DataChangedEvent event) {
return ListenerAssistedType.CREATE_DATABASE == ListenerAssistedType.valueOf(event.getValue())
? new CreateDatabaseListenerAssistedEvent(databaseName)
: new DropDatabaseListenerAssistedEvent(databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssisted;
import org.apache.shardingsphere.mode.persist.pojo.ListenerAssistedType;
import org.apache.shardingsphere.mode.persist.ListenerAssistedType;
import org.apache.shardingsphere.mode.persist.service.ListenerAssistedPersistService;
import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
import org.apache.shardingsphere.mode.spi.PersistRepository;
Expand Down Expand Up @@ -70,13 +69,13 @@ public ClusterMetaDataManagerPersistService(final PersistRepository repository,
@Override
public void createDatabase(final String databaseName) {
metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().add(databaseName);
listenerAssistedPersistService.persistDatabaseNameListenerAssisted(new ListenerAssisted(databaseName, ListenerAssistedType.CREATE_DATABASE));
listenerAssistedPersistService.persistDatabaseNameListenerAssisted(databaseName, ListenerAssistedType.CREATE_DATABASE);
}

@Override
public void dropDatabase(final String databaseName) {
String droppedDatabaseName = metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName).getName();
listenerAssistedPersistService.persistDatabaseNameListenerAssisted(new ListenerAssisted(droppedDatabaseName, ListenerAssistedType.DROP_DATABASE));
listenerAssistedPersistService.persistDatabaseNameListenerAssisted(droppedDatabaseName, ListenerAssistedType.DROP_DATABASE);
metaDataPersistService.getDatabaseMetaDataFacade().getDatabase().drop(droppedDatabaseName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ void assertGetSubscribedKey() {

@Test
void assertBuildCreateDatabaseListenerAssistedEvent() {
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "{databaseName: foo_db, listenerAssistedType: CREATE_DATABASE}", Type.ADDED));
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "CREATE_DATABASE", Type.ADDED));
assertTrue(actual.isPresent());
assertThat(((CreateDatabaseListenerAssistedEvent) actual.get()).getDatabaseName(), is("foo_db"));
}

@Test
void assertBuildDropDatabaseListenerAssistedEvent() {
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "{databaseName: foo_db, listenerAssistedType: DROP_DATABASE}", Type.ADDED));
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "DROP_DATABASE", Type.ADDED));
assertTrue(actual.isPresent());
assertThat(((DropDatabaseListenerAssistedEvent) actual.get()).getDatabaseName(), is("foo_db"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.metadata.MetaDataContextManager;
import org.apache.shardingsphere.mode.persist.ListenerAssistedType;
import org.apache.shardingsphere.mode.persist.service.ListenerAssistedPersistService;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
Expand Down Expand Up @@ -78,14 +79,14 @@ void setUp() {
void assertCreateDatabase() {
metaDataManagerPersistService.createDatabase("foo_db");
verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).add("foo_db");
verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted(any());
verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted("foo_db", ListenerAssistedType.CREATE_DATABASE);
}

@Test
void assertDropDatabase() {
when(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db");
metaDataManagerPersistService.dropDatabase("foo_db");
verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted(any());
verify(listenerAssistedPersistService).persistDatabaseNameListenerAssisted("foo_db", ListenerAssistedType.DROP_DATABASE);
verify(metaDataPersistService.getDatabaseMetaDataFacade().getDatabase()).drop("foo_db");
}

Expand Down

0 comments on commit c627064

Please sign in to comment.