You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/06/09 09:27:46 UTC
[shardingsphere] branch master updated: Add NewYamlTrafficRuleConfigurationSwapper to adpate new metadata structure (#26216)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 f7a8b552f94 Add NewYamlTrafficRuleConfigurationSwapper to adpate new metadata structure (#26216)
f7a8b552f94 is described below
commit f7a8b552f94dcfb56aaf66216cc63bfb6460b432
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Fri Jun 9 17:27:35 2023 +0800
Add NewYamlTrafficRuleConfigurationSwapper to adpate new metadata structure (#26216)
* Add NewYamlTrafficRuleConfigurationSwapper to adpate new metadata structure
* Fix checkstyle
---
.../NewYamlAuthorityRuleConfigurationSwapper.java | 15 ++-
.../metadata/converter/TrafficNodeConverter.java | 57 +++++++++++
.../NewYamlTrafficRuleConfigurationSwapper.java | 114 +++++++++++++++++++++
...NewYamlTrafficRuleConfigurationSwapperTest.java | 34 ++++++
4 files changed, 219 insertions(+), 1 deletion(-)
diff --git a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
index f1723f8959f..ec477c08eeb 100644
--- a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
+++ b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
@@ -23,6 +23,8 @@ import org.apache.shardingsphere.authority.converter.YamlUsersConfigurationConve
import org.apache.shardingsphere.authority.metadata.converter.AuthorityNodeConverter;
import org.apache.shardingsphere.authority.rule.builder.DefaultAuthorityRuleConfigurationBuilder;
import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
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;
@@ -64,11 +66,22 @@ public final class NewYamlAuthorityRuleConfigurationSwapper implements NewYamlRu
if (!version.isPresent()) {
continue;
}
- return YamlEngine.unmarshal(each.getValue(), AuthorityRuleConfiguration.class);
+ return swapToObject(YamlEngine.unmarshal(each.getValue(), YamlAuthorityRuleConfiguration.class));
}
return new AuthorityRuleConfiguration(Collections.emptyList(), new DefaultAuthorityRuleConfigurationBuilder().build().getAuthorityProvider(), "");
}
+ private AuthorityRuleConfiguration swapToObject(final YamlAuthorityRuleConfiguration yamlConfig) {
+ Collection<ShardingSphereUser> users = YamlUsersConfigurationConverter.convertToShardingSphereUser(yamlConfig.getUsers());
+ AlgorithmConfiguration provider = algorithmSwapper.swapToObject(yamlConfig.getPrivilege());
+ if (null == provider) {
+ provider = new DefaultAuthorityRuleConfigurationBuilder().build().getAuthorityProvider();
+ }
+ AuthorityRuleConfiguration result = new AuthorityRuleConfiguration(users, provider, yamlConfig.getDefaultAuthenticator());
+ yamlConfig.getAuthenticators().forEach((key, value) -> result.getAuthenticators().put(key, algorithmSwapper.swapToObject(value)));
+ return result;
+ }
+
@Override
public Class<AuthorityRuleConfiguration> getTypeClass() {
return AuthorityRuleConfiguration.class;
diff --git a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/metadata/converter/TrafficNodeConverter.java b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/metadata/converter/TrafficNodeConverter.java
new file mode 100644
index 00000000000..d2a3bdc1b3e
--- /dev/null
+++ b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/metadata/converter/TrafficNodeConverter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.traffic.metadata.converter;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Traffic node converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TrafficNodeConverter {
+
+ private static final String ROOT_NODE = "traffic/";
+
+ private static final String VERSION_NODE_PREFIX = "/rules/" + ROOT_NODE;
+
+ /**
+ * Get group name path.
+ *
+ * @return group name path
+ */
+ public static String getRootNode() {
+ return ROOT_NODE;
+ }
+
+ /**
+ * Get version.
+ *
+ * @param rulePath rule path
+ * @return version
+ */
+ public static Optional<String> getVersion(final String rulePath) {
+ Pattern pattern = Pattern.compile(VERSION_NODE_PREFIX + "versions" + "/([\\w\\-]+)$", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
+ }
+}
diff --git a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..4503d10881a
--- /dev/null
+++ b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapper.java
@@ -0,0 +1,114 @@
+/*
+ * 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.traffic.yaml.swapper;
+
+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.traffic.api.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.constant.TrafficOrder;
+import org.apache.shardingsphere.traffic.metadata.converter.TrafficNodeConverter;
+import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map.Entry;
+import java.util.Optional;
+
+/**
+ * TODO Rename YamlTrafficRuleConfigurationSwapper when metadata structure adjustment completed. #25485
+ * New YAML traffic rule configuration swapper.
+ */
+public final class NewYamlTrafficRuleConfigurationSwapper implements NewYamlRuleConfigurationSwapper<TrafficRuleConfiguration> {
+
+ private final YamlTrafficStrategyConfigurationSwapper strategySwapper = new YamlTrafficStrategyConfigurationSwapper();
+
+ private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
+
+ @Override
+ public Collection<YamlDataNode> swapToDataNodes(final TrafficRuleConfiguration data) {
+ return Collections.singletonList(new YamlDataNode(TrafficNodeConverter.getRootNode(), YamlEngine.marshal(swapToYamlConfiguration(data))));
+ }
+
+ private YamlTrafficRuleConfiguration swapToYamlConfiguration(final TrafficRuleConfiguration data) {
+ YamlTrafficRuleConfiguration result = new YamlTrafficRuleConfiguration();
+ data.getTrafficStrategies().forEach(each -> result.getTrafficStrategies().put(each.getName(), strategySwapper.swapToYamlConfiguration(each)));
+ setYamlAlgorithms(data, result);
+ return result;
+ }
+
+ private void setYamlAlgorithms(final TrafficRuleConfiguration data, final YamlTrafficRuleConfiguration yamlConfig) {
+ if (null != data.getTrafficAlgorithms()) {
+ data.getTrafficAlgorithms().forEach((key, value) -> yamlConfig.getTrafficAlgorithms().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
+ }
+ if (null != data.getLoadBalancers()) {
+ data.getLoadBalancers().forEach((key, value) -> yamlConfig.getLoadBalancers().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
+ }
+ }
+
+ @Override
+ public TrafficRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
+ TrafficRuleConfiguration result = new TrafficRuleConfiguration();
+ for (YamlDataNode each : dataNodes) {
+ Optional<String> version = TrafficNodeConverter.getVersion(each.getKey());
+ if (!version.isPresent()) {
+ continue;
+ }
+ return swapToObject(YamlEngine.unmarshal(each.getValue(), YamlTrafficRuleConfiguration.class));
+ }
+ return result;
+ }
+
+ private TrafficRuleConfiguration swapToObject(final YamlTrafficRuleConfiguration yamlConfig) {
+
+ TrafficRuleConfiguration result = new TrafficRuleConfiguration();
+ for (Entry<String, YamlTrafficStrategyConfiguration> entry : yamlConfig.getTrafficStrategies().entrySet()) {
+ YamlTrafficStrategyConfiguration strategyConfig = entry.getValue();
+ strategyConfig.setName(entry.getKey());
+ result.getTrafficStrategies().add(strategySwapper.swapToObject(strategyConfig));
+ }
+ setAlgorithms(yamlConfig, result);
+ return result;
+ }
+
+ private void setAlgorithms(final YamlTrafficRuleConfiguration yamlConfig, final TrafficRuleConfiguration ruleConfig) {
+ if (null != yamlConfig.getTrafficAlgorithms()) {
+ yamlConfig.getTrafficAlgorithms().forEach((key, value) -> ruleConfig.getTrafficAlgorithms().put(key, algorithmSwapper.swapToObject(value)));
+ }
+ if (null != yamlConfig.getLoadBalancers()) {
+ yamlConfig.getLoadBalancers().forEach((key, value) -> ruleConfig.getLoadBalancers().put(key, algorithmSwapper.swapToObject(value)));
+ }
+ }
+
+ @Override
+ public Class<TrafficRuleConfiguration> getTypeClass() {
+ return TrafficRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "TRAFFIC";
+ }
+
+ @Override
+ public int getOrder() {
+ return TrafficOrder.ORDER;
+ }
+}
diff --git a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..9c2ddd48cfe
--- /dev/null
+++ b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/NewYamlTrafficRuleConfigurationSwapperTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.traffic.yaml.swapper;
+
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class NewYamlTrafficRuleConfigurationSwapperTest {
+
+ private final NewYamlTrafficRuleConfigurationSwapper swapper = new NewYamlTrafficRuleConfigurationSwapper();
+
+ @Test
+ void assertSwapToDataNodes() {
+ assertThat(swapper.swapToDataNodes(new TrafficRuleConfiguration()).iterator().next().getKey(), is("traffic/"));
+ }
+}