|
| 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.service.manager; |
| 19 | + |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; |
| 22 | +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; |
| 23 | +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; |
| 24 | +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; |
| 25 | +import org.apache.shardingsphere.mode.metadata.MetaDataContexts; |
| 26 | +import org.apache.shardingsphere.mode.service.PersistServiceFacade; |
| 27 | +import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor; |
| 28 | + |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | + |
| 31 | +/** |
| 32 | + * Rule item manager. |
| 33 | + */ |
| 34 | +@RequiredArgsConstructor |
| 35 | +public class RuleItemManager { |
| 36 | + |
| 37 | + private final AtomicReference<MetaDataContexts> metaDataContexts; |
| 38 | + |
| 39 | + private final PersistServiceFacade persistServiceFacade; |
| 40 | + |
| 41 | + private final ConfigurationManager configurationManager; |
| 42 | + |
| 43 | + /** |
| 44 | + * Alter with rule item. |
| 45 | + * |
| 46 | + * @param event alter rule item event |
| 47 | + */ |
| 48 | + @SuppressWarnings({"rawtypes", "unchecked", "unused"}) |
| 49 | + public void alterRuleItem(final AlterRuleItemEvent event) { |
| 50 | + if (!event.getActiveVersion().equals(persistServiceFacade.getMetaDataPersistService().getMetaDataVersionPersistService() |
| 51 | + .getActiveVersionByFullPath(event.getActiveVersionKey()))) { |
| 52 | + return; |
| 53 | + } |
| 54 | + RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType()); |
| 55 | + String yamlContent = persistServiceFacade.getMetaDataPersistService().getMetaDataVersionPersistService() |
| 56 | + .getVersionPathByActiveVersion(event.getActiveVersionKey(), event.getActiveVersion()); |
| 57 | + String databaseName = event.getDatabaseName(); |
| 58 | + RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.get().getMetaData().getDatabase(databaseName)); |
| 59 | + synchronized (this) { |
| 60 | + processor.changeRuleItemConfiguration(event, currentRuleConfig, processor.swapRuleItemConfiguration(event, yamlContent)); |
| 61 | + configurationManager.alterRuleConfiguration(databaseName, currentRuleConfig); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Drop with rule item. |
| 67 | + * |
| 68 | + * @param event drop rule item event |
| 69 | + */ |
| 70 | + @SuppressWarnings({"rawtypes", "unchecked", "unused"}) |
| 71 | + public void dropRuleItem(final DropRuleItemEvent event) { |
| 72 | + String databaseName = event.getDatabaseName(); |
| 73 | + if (!metaDataContexts.get().getMetaData().containsDatabase(databaseName)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType()); |
| 77 | + RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.get().getMetaData().getDatabase(databaseName)); |
| 78 | + synchronized (this) { |
| 79 | + processor.dropRuleItemConfiguration(event, currentRuleConfig); |
| 80 | + configurationManager.dropRuleConfiguration(databaseName, currentRuleConfig); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments