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

fix datasource closed exception for db-discovery #17774

Merged
merged 1 commit into from
May 18, 2022
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 @@ -31,10 +31,12 @@
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.rule.event.DataSourceStatusChangedEvent;
import org.apache.shardingsphere.infra.rule.identifier.scope.SchemaRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
import org.apache.shardingsphere.infra.rule.identifier.type.InstanceAwareRule;
import org.apache.shardingsphere.infra.rule.identifier.type.StatusContainedRule;
import org.apache.shardingsphere.mode.metadata.storage.event.DataSourceNameDisabledEvent;
import org.apache.shardingsphere.mode.metadata.storage.event.PrimaryDataSourceChangedEvent;
Expand All @@ -58,13 +60,17 @@
/**
* Database discovery rule.
*/
public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContainedRule, StatusContainedRule, ExportableRule {
public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContainedRule, StatusContainedRule, ExportableRule, InstanceAwareRule {

private final Map<String, DatabaseDiscoveryProviderAlgorithm> discoveryTypes;

@Getter
private final Map<String, DatabaseDiscoveryDataSourceRule> dataSourceRules;

private final String databaseName;

private final Map<String, DataSource> dataSourceMap;

public DatabaseDiscoveryRule(final String databaseName, final Map<String, DataSource> dataSourceMap, final DatabaseDiscoveryRuleConfiguration config) {
this(databaseName, dataSourceMap, config.getDataSources(), config.getDiscoveryHeartbeats(), getDiscoveryProviderAlgorithms(config.getDiscoveryTypes()));
}
Expand All @@ -76,10 +82,11 @@ public DatabaseDiscoveryRule(final String databaseName, final Map<String, DataSo
private DatabaseDiscoveryRule(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<DatabaseDiscoveryDataSourceRuleConfiguration> dataSourceRuleConfigs,
final Map<String, DatabaseDiscoveryHeartBeatConfiguration> heartBeatConfig, final Map<String, DatabaseDiscoveryProviderAlgorithm> discoveryTypes) {
this.discoveryTypes = discoveryTypes;
this.databaseName = databaseName;
this.dataSourceMap = dataSourceMap;
dataSourceRules = getDataSourceRules(dataSourceRuleConfigs, heartBeatConfig);
findMasterSlaveRelation(databaseName, dataSourceMap);
initAware();
initHeartBeatJobs(databaseName, dataSourceMap);
}

private static Map<String, DatabaseDiscoveryProviderAlgorithm> getDiscoveryProviderAlgorithms(final Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypesConfig) {
Expand Down Expand Up @@ -178,8 +185,8 @@ private Map<String, String> exportPrimaryDataSourceMap() {
return result;
}

private void initHeartBeatJobs(final String databaseName, final Map<String, DataSource> dataSourceMap) {
Optional<ModeScheduleContext> modeScheduleContext = ModeScheduleContextFactory.getInstance().get();
private void initHeartBeatJobs(final InstanceContext instanceContext) {
Optional<ModeScheduleContext> modeScheduleContext = ModeScheduleContextFactory.getInstance().get(instanceContext.getInstance().getInstanceDefinition().getInstanceId().getId());
if (modeScheduleContext.isPresent()) {
for (Entry<String, DatabaseDiscoveryDataSourceRule> entry : dataSourceRules.entrySet()) {
DatabaseDiscoveryDataSourceRule rule = entry.getValue();
Expand All @@ -197,4 +204,9 @@ private void initHeartBeatJobs(final String databaseName, final Map<String, Data
public String getType() {
return DatabaseDiscoveryRule.class.getSimpleName();
}

@Override
public void setInstanceContext(final InstanceContext instanceContext) {
initHeartBeatJobs(instanceContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void init(final String instanceId, final ModeConfiguration modeConfig) {
/**
* Get mode schedule context of current instance.
*
* @param instanceId instance id
* @return mode schedule context
*/
public Optional<ModeScheduleContext> get() {
// TODO get by instance id
public Optional<ModeScheduleContext> get(final String instanceId) {
if (modeScheduleContexts.values().isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(modeScheduleContexts.values().iterator().next());
return Optional.ofNullable(modeScheduleContexts.get(instanceId));
}

/**
Expand Down