Skip to content

Commit

Permalink
Refactor StateChangedSubscriber (apache#31005)
Browse files Browse the repository at this point in the history
* Refactor StateChangedSubscriber

* Refactor StorageNodeStateChangedWatcher
  • Loading branch information
terrymanu authored Apr 25, 2024
1 parent 82605f5 commit 0a06b5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ public Optional<GovernanceEvent> createGovernanceEvent(final DataChangedEvent ev
}
Optional<QualifiedDataSource> qualifiedDataSource = StorageNode.extractQualifiedDataSource(event.getKey());
if (qualifiedDataSource.isPresent()) {
QualifiedDataSource database = qualifiedDataSource.get();
StorageNodeDataSource storageNodeDataSource = new YamlStorageNodeDataSourceSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlStorageNodeDataSource.class));
return Optional.of(new StorageNodeChangedEvent(database, storageNodeDataSource));
return Optional.of(new StorageNodeChangedEvent(qualifiedDataSource.get(), storageNodeDataSource));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

import com.google.common.eventbus.Subscribe;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager;
Expand Down Expand Up @@ -58,15 +59,16 @@ public StateChangedSubscriber(final RegistryCenter registryCenter, final Context
*/
@Subscribe
public synchronized void renew(final StorageNodeChangedEvent event) {
ShardingSphereMetaData metaData = contextManager.getMetaDataContexts().getMetaData();
if (!metaData.containsDatabase(event.getQualifiedDataSource().getDatabaseName())) {
QualifiedDataSource qualifiedDataSource = event.getQualifiedDataSource();
if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(qualifiedDataSource.getDatabaseName())) {
return;
}
for (StaticDataSourceRuleAttribute each : metaData.getDatabase(event.getQualifiedDataSource().getDatabaseName()).getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) {
each.updateStatus(event.getQualifiedDataSource(), event.getDataSource().getStatus());
}
DataSourceStateManager.getInstance().updateState(
event.getQualifiedDataSource().getDatabaseName(), event.getQualifiedDataSource().getDataSourceName(), DataSourceState.valueOf(event.getDataSource().getStatus().name()));
qualifiedDataSource.getDatabaseName(), qualifiedDataSource.getDataSourceName(), DataSourceState.valueOf(event.getDataSource().getStatus().name()));
ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(qualifiedDataSource.getDatabaseName());
for (StaticDataSourceRuleAttribute each : database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) {
each.updateStatus(qualifiedDataSource, event.getDataSource().getStatus());
}
}

/**
Expand Down

0 comments on commit 0a06b5b

Please sign in to comment.