View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Rule item changed subscriber.
34   */
35  @RequiredArgsConstructor
36  public final class RuleItemChangedSubscriber {
37      
38      private final ContextManager contextManager;
39      
40      /**
41       * Renew with alter rule item.
42       *
43       * @param event alter rule item event
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              // TODO Remove isCluster judgment
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       * Renew with drop rule item.
69       *
70       * @param event drop rule item event
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              // TODO Remove isCluster judgment
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  }