1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.mode.subsciber;
19
20 import com.google.common.eventbus.Subscribe;
21 import lombok.RequiredArgsConstructor;
22 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
23 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
24 import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
25 import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
26 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
27 import org.apache.shardingsphere.mode.event.config.AlterDatabaseRuleConfigurationEvent;
28 import org.apache.shardingsphere.mode.event.config.DropDatabaseRuleConfigurationEvent;
29 import org.apache.shardingsphere.mode.manager.ContextManager;
30 import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
31
32
33
34
35 @RequiredArgsConstructor
36 public final class RuleItemChangedSubscriber {
37
38 private final ContextManager contextManager;
39
40
41
42
43
44
45 @SuppressWarnings({"rawtypes", "unchecked", "unused"})
46 @Subscribe
47 public void renew(final AlterRuleItemEvent event) {
48 if (!event.getActiveVersion().equals(contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey()))) {
49 return;
50 }
51 RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType());
52 String yamlContent =
53 contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().getVersionPathByActiveVersion(event.getActiveVersionKey(), event.getActiveVersion());
54 ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(event.getDatabaseName());
55 RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(database);
56 synchronized (this) {
57 processor.changeRuleItemConfiguration(event, currentRuleConfig, processor.swapRuleItemConfiguration(event, yamlContent));
58
59 if (contextManager.getInstanceContext().isCluster()) {
60 contextManager.getInstanceContext().getEventBusContext().post(new AlterDatabaseRuleConfigurationEvent(event.getDatabaseName(), currentRuleConfig));
61 return;
62 }
63 contextManager.getConfigurationContextManager().alterRuleConfiguration(event.getDatabaseName(), currentRuleConfig);
64 }
65 }
66
67
68
69
70
71
72 @SuppressWarnings({"rawtypes", "unchecked", "unused"})
73 @Subscribe
74 public void renew(final DropRuleItemEvent event) {
75 if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) {
76 return;
77 }
78 RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType());
79 ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(event.getDatabaseName());
80 RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(database);
81 synchronized (this) {
82 processor.dropRuleItemConfiguration(event, currentRuleConfig);
83
84 if (contextManager.getInstanceContext().isCluster()) {
85 contextManager.getInstanceContext().getEventBusContext().post(new DropDatabaseRuleConfigurationEvent(event.getDatabaseName(), currentRuleConfig));
86 return;
87 }
88 contextManager.getConfigurationContextManager().dropRuleConfiguration(event.getDatabaseName(), currentRuleConfig);
89 }
90 }
91 }