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 DatabaseMetaDataChangedListener #34053

Merged
merged 1 commit into from
Dec 14, 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 @@ -17,7 +17,6 @@

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

import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode;
Expand Down Expand Up @@ -52,7 +51,7 @@ public final class DatabaseMetaDataChangedListener implements DataChangedEventLi

private final EventBusContext eventBusContext;

private final RuleConfigurationEventBuilder builder = new RuleConfigurationEventBuilder();
private final RuleConfigurationEventBuilder eventBuilder = new RuleConfigurationEventBuilder();

@Override
public void onChange(final DataChangedEvent event) {
Expand All @@ -70,16 +69,16 @@ private Optional<DispatchEvent> createDispatchEvent(final DataChangedEvent event
return createSchemaChangedEvent(databaseName.get(), schemaName.get(), event);
}
schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(key);
if (schemaName.isPresent() && tableMetaDataChanged(event.getKey())) {
if (schemaName.isPresent() && isTableMetaDataChanged(event.getKey())) {
return createTableChangedEvent(databaseName.get(), schemaName.get(), event);
}
if (schemaName.isPresent() && viewMetaDataChanged(event.getKey())) {
if (schemaName.isPresent() && isViewMetaDataChanged(event.getKey())) {
return createViewChangedEvent(databaseName.get(), schemaName.get(), event);
}
if (DataSourceMetaDataNode.isDataSourcesNode(key)) {
return createDataSourceEvent(databaseName.get(), event);
return createDataSourceChangedEvent(databaseName.get(), event);
}
return builder.build(databaseName.get(), event);
return eventBuilder.build(databaseName.get(), event);
}

private Optional<DispatchEvent> createSchemaChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) {
Expand All @@ -94,43 +93,39 @@ private Optional<DispatchEvent> createSchemaChangedEvent(final String databaseNa
}
}

private boolean tableMetaDataChanged(final String key) {
private boolean isTableMetaDataChanged(final String key) {
return TableMetaDataNode.isTableActiveVersionNode(key) || TableMetaDataNode.isTableNode(key);
}

private Optional<DispatchEvent> createTableChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) {
if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && TableMetaDataNode.isTableActiveVersionNode(event.getKey())) {
Optional<String> tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey());
Preconditions.checkState(tableName.isPresent(), "Not found table name.");
return Optional.of(new CreateOrAlterTableEvent(databaseName, schemaName, tableName.get(), event.getKey(), event.getValue()));
String tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found."));
return Optional.of(new CreateOrAlterTableEvent(databaseName, schemaName, tableName, event.getKey(), event.getValue()));
}
if (Type.DELETED == event.getType() && TableMetaDataNode.isTableNode(event.getKey())) {
Optional<String> tableName = TableMetaDataNode.getTableName(event.getKey());
Preconditions.checkState(tableName.isPresent(), "Not found table name.");
return Optional.of(new DropTableEvent(databaseName, schemaName, tableName.get()));
String tableName = TableMetaDataNode.getTableName(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found."));
return Optional.of(new DropTableEvent(databaseName, schemaName, tableName));
}
return Optional.empty();
}

private boolean viewMetaDataChanged(final String key) {
private boolean isViewMetaDataChanged(final String key) {
return ViewMetaDataNode.isViewActiveVersionNode(key) || ViewMetaDataNode.isViewNode(key);
}

private Optional<DispatchEvent> createViewChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) {
if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && ViewMetaDataNode.isViewActiveVersionNode(event.getKey())) {
Optional<String> viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey());
Preconditions.checkState(viewName.isPresent(), "Not found view name.");
return Optional.of(new CreateOrAlterViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue()));
String viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found."));
return Optional.of(new CreateOrAlterViewEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue()));
}
if (Type.DELETED == event.getType() && ViewMetaDataNode.isViewNode(event.getKey())) {
Optional<String> viewName = ViewMetaDataNode.getViewName(event.getKey());
Preconditions.checkState(viewName.isPresent(), "Not found view name.");
return Optional.of(new DropViewEvent(databaseName, schemaName, viewName.get(), event.getKey(), event.getValue()));
String viewName = ViewMetaDataNode.getViewName(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found."));
return Optional.of(new DropViewEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue()));
}
return Optional.empty();
}

private Optional<DispatchEvent> createDataSourceEvent(final String databaseName, final DataChangedEvent event) {
private Optional<DispatchEvent> createDataSourceChangedEvent(final String databaseName, final DataChangedEvent event) {
if (DataSourceMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceUnitNode(event.getKey())) {
return createStorageUnitChangedEvent(databaseName, event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public final class GlobalMetaDataChangedListener implements DataChangedEventList

private final EventBusContext eventBusContext;

private final DispatchEventBuilder<?> builder;
private final DispatchEventBuilder<?> eventBuilder;

@Override
public void onChange(final DataChangedEvent event) {
if (builder.getSubscribedTypes().contains(event.getType())) {
builder.build(event).ifPresent(eventBusContext::post);
if (eventBuilder.getSubscribedTypes().contains(event.getType())) {
eventBuilder.build(event).ifPresent(eventBusContext::post);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
*/
public final class StandaloneMetaDataManagerPersistService implements MetaDataManagerPersistService {

private final RuleConfigurationEventBuilder ruleConfigurationEventBuilder = new RuleConfigurationEventBuilder();
private final RuleConfigurationEventBuilder ruleConfigEventBuilder = new RuleConfigurationEventBuilder();

private final MetaDataPersistService metaDataPersistService;

Expand Down Expand Up @@ -302,7 +302,7 @@ public void alterRuleConfiguration(final String databaseName, final RuleConfigur
}

private Optional<DispatchEvent> buildAlterRuleItemEvent(final String databaseName, final MetaDataVersion metaDataVersion, final Type type) {
return ruleConfigurationEventBuilder.build(databaseName, new DataChangedEvent(metaDataVersion.getActiveVersionNodePath(), metaDataVersion.getNextActiveVersion(), type));
return ruleConfigEventBuilder.build(databaseName, new DataChangedEvent(metaDataVersion.getActiveVersionNodePath(), metaDataVersion.getNextActiveVersion(), type));
}

@Override
Expand Down
Loading