You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/01 11:11:03 UTC
[shardingsphere] branch master updated: Add new shadow rule configuration swapper (#25992)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 cecf547c7bf Add new shadow rule configuration swapper (#25992)
cecf547c7bf is described below
commit cecf547c7bf4bca10a092a528a98235998bf6bf1
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Thu Jun 1 19:10:55 2023 +0800
Add new shadow rule configuration swapper (#25992)
---
features/shadow/core/pom.xml | 5 ++
.../NewYamlShadowRuleConfigurationSwapper.java | 86 ++++++++++++++++++++++
.../NewYamlShadowRuleConfigurationSwapperTest.java | 75 +++++++++++++++++++
.../config/shadow/ShadowNodeConverter.java | 76 +++++++++++++++++++
4 files changed, 242 insertions(+)
diff --git a/features/shadow/core/pom.xml b/features/shadow/core/pom.xml
index 350042f9fb9..5346cfd198b 100644
--- a/features/shadow/core/pom.xml
+++ b/features/shadow/core/pom.xml
@@ -49,6 +49,11 @@
<artifactId>shardingsphere-infra-rewrite</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-metadata-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapper.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..4ff2a107e84
--- /dev/null
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapper.java
@@ -0,0 +1,86 @@
+/*
+ * 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.shadow.yaml.swapper;
+
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
+import org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapper;
+import org.apache.shardingsphere.metadata.persist.node.metadata.config.shadow.ShadowNodeConverter;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.constant.ShadowOrder;
+import org.apache.shardingsphere.shadow.yaml.swapper.table.YamlShadowTableConfigurationSwapper;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Map.Entry;
+
+/**
+ * TODO Rename YamlShadowRuleConfigurationSwapper when metadata structure adjustment completed.
+ * New YAML shadow rule configuration swapper.
+ */
+public final class NewYamlShadowRuleConfigurationSwapper implements NewYamlRuleConfigurationSwapper<ShadowRuleConfiguration> {
+
+ // TODO to be used
+ private final YamlShadowTableConfigurationSwapper tableConfigurationSwapper = new YamlShadowTableConfigurationSwapper();
+
+ private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
+
+ @Override
+ public Collection<YamlDataNode> swapToDataNodes(final ShadowRuleConfiguration data) {
+ Collection<YamlDataNode> result = new LinkedHashSet<>();
+ for (ShadowDataSourceConfiguration each : data.getDataSources()) {
+ result.add(new YamlDataNode(ShadowNodeConverter.getDataSourcePath(each.getName()), YamlEngine.marshal(each)));
+ }
+ for (Entry<String, ShadowTableConfiguration> entry : data.getTables().entrySet()) {
+ result.add(new YamlDataNode(ShadowNodeConverter.getTableNamePath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
+ }
+ for (Entry<String, AlgorithmConfiguration> entry : data.getShadowAlgorithms().entrySet()) {
+ result.add(new YamlDataNode(ShadowNodeConverter.getShadowAlgorithmPath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
+ }
+ if (!Strings.isNullOrEmpty(data.getDefaultShadowAlgorithmName())) {
+ result.add(new YamlDataNode(ShadowNodeConverter.getDefaultShadowAlgorithmPath(), YamlEngine.marshal(data.getDefaultShadowAlgorithmName())));
+ }
+ return result;
+ }
+
+ @Override
+ public ShadowRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
+ // TODO to be completed
+ return new ShadowRuleConfiguration();
+ }
+
+ @Override
+ public Class<ShadowRuleConfiguration> getTypeClass() {
+ return ShadowRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "SHADOW";
+ }
+
+ @Override
+ public int getOrder() {
+ return ShadowOrder.ORDER;
+ }
+}
diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..c41e9798d48
--- /dev/null
+++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/swapper/NewYamlShadowRuleConfigurationSwapperTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.shadow.yaml.swapper;
+
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class NewYamlShadowRuleConfigurationSwapperTest {
+
+ private final NewYamlShadowRuleConfigurationSwapper swapper = new NewYamlShadowRuleConfigurationSwapper();
+
+ @Test
+ void assertSwapEmptyConfigToDataNodes() {
+ ShadowRuleConfiguration config = new ShadowRuleConfiguration();
+ Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+ assertThat(result.size(), is(0));
+ }
+
+ @Test
+ void assertSwapFullConfigToDataNodes() {
+ ShadowRuleConfiguration config = createMaximumShadowRule();
+ Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+ assertThat(result.size(), is(4));
+ Iterator<YamlDataNode> iterator = result.iterator();
+ assertThat(iterator.next().getKey(), is("data_sources/foo"));
+ assertThat(iterator.next().getKey(), is("tables/foo_table"));
+ assertThat(iterator.next().getKey(), is("shadow_algorithms/FIXTURE"));
+ assertThat(iterator.next().getKey(), is("default_shadow_algorithm_name"));
+ }
+
+ private ShadowRuleConfiguration createMaximumShadowRule() {
+ Collection<ShadowDataSourceConfiguration> dataSources = new LinkedList<>();
+ dataSources.add(new ShadowDataSourceConfiguration("foo", "ds_0", "ds_1"));
+ Map<String, ShadowTableConfiguration> tables = new LinkedHashMap<>();
+ tables.put("foo_table", new ShadowTableConfiguration(Collections.singleton("ds_0"), Collections.singleton("FIXTURE")));
+ Map<String, AlgorithmConfiguration> shadowAlgorithms = new LinkedHashMap<>();
+ shadowAlgorithms.put("FIXTURE", new AlgorithmConfiguration("FIXTURE", new Properties()));
+ ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+ result.setDataSources(dataSources);
+ result.setTables(tables);
+ result.setShadowAlgorithms(shadowAlgorithms);
+ result.setDefaultShadowAlgorithmName("FIXTURE");
+ return result;
+ }
+}
diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/shadow/ShadowNodeConverter.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/shadow/ShadowNodeConverter.java
new file mode 100644
index 00000000000..d585dffc9cc
--- /dev/null
+++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/shadow/ShadowNodeConverter.java
@@ -0,0 +1,76 @@
+/*
+ * 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.metadata.persist.node.metadata.config.shadow;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * TODO move to feature
+ * Sharding node converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShadowNodeConverter {
+
+ private static final String DATA_SOURCES = "data_sources";
+
+ private static final String TABLES = "tables";
+
+ private static final String SHADOW_ALGORITHMS = "shadow_algorithms";
+
+ private static final String DEFAULT_SHADOW_ALGORITHM_NAME = "default_shadow_algorithm_name";
+
+ /**
+ * Get data source path.
+ *
+ * @param dataSourceName data source name
+ * @return data source path
+ */
+ public static String getDataSourcePath(final String dataSourceName) {
+ return String.join("/", DATA_SOURCES, dataSourceName);
+ }
+
+ /**
+ * Get table name path.
+ *
+ * @param tableName table name
+ * @return table name path
+ */
+ public static String getTableNamePath(final String tableName) {
+ return String.join("/", TABLES, tableName);
+ }
+
+ /**
+ * Get shadow algorithm path.
+ *
+ * @param shadowAlgorithmName shadow algorithm name
+ * @return shadow algorithm path
+ */
+ public static String getShadowAlgorithmPath(final String shadowAlgorithmName) {
+ return String.join("/", SHADOW_ALGORITHMS, shadowAlgorithmName);
+ }
+
+ /**
+ * Get default shadow algorithm path.
+ *
+ * @return default shadow algorithm path
+ */
+ public static String getDefaultShadowAlgorithmPath() {
+ return String.join("/", DEFAULT_SHADOW_ALGORITHM_NAME);
+ }
+}