You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2021/01/04 07:32:35 UTC
[shardingsphere] branch master updated: Add cache manager for
configuration changes (#8846)
This is an automated email from the ASF dual-hosted git repository.
kimmking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 583fac5 Add cache manager for configuration changes (#8846)
583fac5 is described below
commit 583fac57195af09d4bf6a84ea5e19ec213ba93dc
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Mon Jan 4 15:32:16 2021 +0800
Add cache manager for configuration changes (#8846)
* Add cache manager for configuration changes
* Add cache manager for configuration changes
* Add cache manager for configuration changes
---
.../governance/core/config/ConfigCacheManager.java | 56 ++++++++++++++++++++++
.../governance/core/config/ConfigCenter.java | 45 +++++++++++------
.../governance/core/config/ConfigCenterTest.java | 5 +-
3 files changed, 89 insertions(+), 17 deletions(-)
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java
new file mode 100644
index 0000000..3b334fc
--- /dev/null
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCacheManager.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.governance.core.config;
+
+import com.google.common.base.Joiner;
+import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
+
+import java.util.UUID;
+
+/**
+ * Config cache manager.
+ */
+public final class ConfigCacheManager {
+
+ private static final String CACHE_KEY = "cache";
+
+ private static final String PATH_SEPARATOR = "/";
+
+ private final ConfigurationRepository repository;
+
+ public ConfigCacheManager(final ConfigurationRepository repository) {
+ this.repository = repository;
+ }
+
+ /**
+ * Cache configuration.
+ *
+ * @param key key
+ * @param configuration configuration
+ * @return cache id
+ */
+ public String cache(final String key, final String configuration) {
+ String cacheId = getCacheId();
+ repository.persist(Joiner.on(PATH_SEPARATOR).join(key, CACHE_KEY, cacheId), configuration);
+ return cacheId;
+ }
+
+ private String getCacheId() {
+ return UUID.randomUUID().toString().replace("-", "");
+ }
+}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
index 7ebe76d..32c4bc5 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
@@ -76,9 +76,12 @@ public final class ConfigCenter {
private final ConfigurationRepository repository;
+ private final ConfigCacheManager configCacheManager;
+
public ConfigCenter(final ConfigurationRepository repository) {
node = new ConfigCenterNode();
this.repository = repository;
+ configCacheManager = new ConfigCacheManager(repository);
ShardingSphereEventBus.getInstance().register(this);
}
@@ -126,7 +129,7 @@ public final class ConfigCenter {
*/
@Subscribe
public synchronized void renew(final RuleConfigurationsPersistEvent event) {
- persistRuleConfigurations(event.getSchemaName(), event.getRuleConfigurations());
+ cacheRuleConfigurations(event.getSchemaName(), event.getRuleConfigurations());
}
/**
@@ -190,17 +193,21 @@ public final class ConfigCenter {
}
private void persistDataSourceConfigurations(final String schemaName, final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
- Map<String, YamlDataSourceConfiguration> yamlDataSourceConfigurations = dataSourceConfigurations.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
- entry -> new DataSourceConfigurationYamlSwapper().swapToYamlConfiguration(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
- YamlDataSourceConfigurationWrap yamlDataSourceConfigWrap = new YamlDataSourceConfigurationWrap();
- yamlDataSourceConfigWrap.setDataSources(yamlDataSourceConfigurations);
- repository.persist(node.getDataSourcePath(schemaName), YamlEngine.marshal(yamlDataSourceConfigWrap));
+ repository.persist(node.getDataSourcePath(schemaName), YamlEngine.marshal(createYamlDataSourceConfigurationWrap(dataSourceConfigurations)));
}
private void addDataSourceConfigurations(final String schemaName, final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
Map<String, DataSourceConfiguration> dataSourceConfigurationMap = loadDataSourceConfigurations(schemaName);
dataSourceConfigurationMap.putAll(dataSourceConfigurations);
- persistDataSourceConfigurations(schemaName, dataSourceConfigurationMap);
+ repository.persist(node.getDataSourcePath(schemaName), YamlEngine.marshal(createYamlDataSourceConfigurationWrap(dataSourceConfigurations)));
+ }
+
+ private YamlDataSourceConfigurationWrap createYamlDataSourceConfigurationWrap(final Map<String, DataSourceConfiguration> dataSourceConfigurations) {
+ Map<String, YamlDataSourceConfiguration> yamlDataSourceConfigurations = dataSourceConfigurations.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
+ entry -> new DataSourceConfigurationYamlSwapper().swapToYamlConfiguration(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+ YamlDataSourceConfigurationWrap result = new YamlDataSourceConfigurationWrap();
+ result.setDataSources(yamlDataSourceConfigurations);
+ return result;
}
private void persistRuleConfigurations(final String schemaName, final Collection<RuleConfiguration> ruleConfigurations, final boolean isOverwrite) {
@@ -210,6 +217,19 @@ public final class ConfigCenter {
}
private void persistRuleConfigurations(final String schemaName, final Collection<RuleConfiguration> ruleConfigurations) {
+ repository.persist(node.getRulePath(schemaName), YamlEngine.marshal(createYamlRootRuleConfigurations(schemaName, ruleConfigurations)));
+ }
+
+ private void cacheRuleConfigurations(final String schemaName, final Collection<RuleConfiguration> ruleConfigurations) {
+ configCacheManager.cache(node.getRulePath(schemaName), YamlEngine.marshal(createYamlRootRuleConfigurations(schemaName, ruleConfigurations)));
+ }
+
+ private void checkDataSources(final String schemaName, final Collection<ReplicaQueryDataSourceRuleConfiguration> dataSources) {
+ dataSources.forEach(each -> Preconditions.checkState(
+ !each.getPrimaryDataSourceName().isEmpty(), "No available replica-query rule configuration in `%s` for governance.", schemaName));
+ }
+
+ private YamlRootRuleConfigurations createYamlRootRuleConfigurations(final String schemaName, final Collection<RuleConfiguration> ruleConfigurations) {
Collection<RuleConfiguration> configs = new LinkedList<>();
for (RuleConfiguration each : ruleConfigurations) {
if (each instanceof ShardingRuleConfiguration) {
@@ -249,14 +269,9 @@ public final class ConfigCenter {
configs.add(each);
}
}
- YamlRootRuleConfigurations yamlRuleConfigs = new YamlRootRuleConfigurations();
- yamlRuleConfigs.setRules(new YamlRuleConfigurationSwapperEngine().swapToYamlConfigurations(configs));
- repository.persist(node.getRulePath(schemaName), YamlEngine.marshal(yamlRuleConfigs));
- }
-
- private void checkDataSources(final String schemaName, final Collection<ReplicaQueryDataSourceRuleConfiguration> dataSources) {
- dataSources.forEach(each -> Preconditions.checkState(
- !each.getPrimaryDataSourceName().isEmpty(), "No available replica-query rule configuration in `%s` for governance.", schemaName));
+ YamlRootRuleConfigurations result = new YamlRootRuleConfigurations();
+ result.setRules(new YamlRuleConfigurationSwapperEngine().swapToYamlConfigurations(configs));
+ return result;
}
private boolean hasAvailableTableConfigurations(final ShardingRuleConfiguration config) {
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
index de83ce2..a8ae641 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
@@ -69,6 +69,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -498,7 +499,7 @@ public final class ConfigCenterTest {
DataSourcePersistEvent event = new DataSourcePersistEvent("sharding_db", createDataSourceConfigurations());
ConfigCenter configCenter = new ConfigCenter(configurationRepository);
configCenter.renew(event);
- verify(configurationRepository).persist(eq("/metadata/sharding_db/datasource"), anyString());
+ verify(configurationRepository).persist(startsWith("/metadata/sharding_db/datasource"), anyString());
}
@Test
@@ -506,7 +507,7 @@ public final class ConfigCenterTest {
RuleConfigurationsPersistEvent event = new RuleConfigurationsPersistEvent("sharding_db", createRuleConfigurations());
ConfigCenter configCenter = new ConfigCenter(configurationRepository);
configCenter.renew(event);
- verify(configurationRepository).persist(eq("/metadata/sharding_db/rule"), anyString());
+ verify(configurationRepository).persist(startsWith("/metadata/sharding_db/rule/cache"), anyString());
}
@Test