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/08 07:35:40 UTC

[shardingsphere] branch master updated: Add NewYamlAuthorityRuleConfigurationSwapper of global rule. (#26163)

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 152abe2693e Add NewYamlAuthorityRuleConfigurationSwapper of global rule. (#26163)
152abe2693e is described below

commit 152abe2693eb91e74a3f835aac653a5c1e190c1f
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Thu Jun 8 15:35:32 2023 +0800

    Add NewYamlAuthorityRuleConfigurationSwapper of global rule. (#26163)
---
 .../metadata/converter/AuthorityNodeConverter.java | 57 ++++++++++++++
 .../NewYamlAuthorityRuleConfigurationSwapper.java  | 86 ++++++++++++++++++++++
 .../metadata/AuthorityNodeConverterTest.java       | 42 +++++++++++
 ...wYamlAuthorityRuleConfigurationSwapperTest.java | 44 +++++++++++
 .../database/NewDatabaseRulePersistService.java    |  5 +-
 5 files changed, 230 insertions(+), 4 deletions(-)

diff --git a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/metadata/converter/AuthorityNodeConverter.java b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/metadata/converter/AuthorityNodeConverter.java
new file mode 100644
index 00000000000..6c72868b607
--- /dev/null
+++ b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/metadata/converter/AuthorityNodeConverter.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.authority.metadata.converter;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Authority node converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class AuthorityNodeConverter {
+    
+    private static final String ROOT_NODE = "authority/";
+    
+    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/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
new file mode 100644
index 00000000000..f1723f8959f
--- /dev/null
+++ b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.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.authority.yaml.swapper;
+
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.authority.constant.AuthorityOrder;
+import org.apache.shardingsphere.authority.converter.YamlUsersConfigurationConverter;
+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.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 java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+/**
+ * TODO Rename YamlAuthorityRuleConfigurationSwapper when metadata structure adjustment completed. #25485
+ * New YAML Authority rule configuration swapper.
+ */
+public final class NewYamlAuthorityRuleConfigurationSwapper implements NewYamlRuleConfigurationSwapper<AuthorityRuleConfiguration> {
+    
+    private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
+    
+    @Override
+    public Collection<YamlDataNode> swapToDataNodes(final AuthorityRuleConfiguration data) {
+        return Collections.singletonList(new YamlDataNode(AuthorityNodeConverter.getRootNode(), YamlEngine.marshal(swapToYamlConfiguration(data))));
+        
+    }
+    
+    private YamlAuthorityRuleConfiguration swapToYamlConfiguration(final AuthorityRuleConfiguration data) {
+        YamlAuthorityRuleConfiguration result = new YamlAuthorityRuleConfiguration();
+        result.setPrivilege(algorithmSwapper.swapToYamlConfiguration(data.getAuthorityProvider()));
+        result.setUsers(YamlUsersConfigurationConverter.convertToYamlUserConfiguration(data.getUsers()));
+        result.setDefaultAuthenticator(data.getDefaultAuthenticator());
+        if (!data.getAuthenticators().isEmpty()) {
+            data.getAuthenticators().forEach((key, value) -> result.getAuthenticators().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
+        }
+        return result;
+    }
+    
+    @Override
+    public AuthorityRuleConfiguration swapToObject(final Collection<YamlDataNode> dataNodes) {
+        for (YamlDataNode each : dataNodes) {
+            Optional<String> version = AuthorityNodeConverter.getVersion(each.getKey());
+            if (!version.isPresent()) {
+                continue;
+            }
+            return YamlEngine.unmarshal(each.getValue(), AuthorityRuleConfiguration.class);
+        }
+        return new AuthorityRuleConfiguration(Collections.emptyList(), new DefaultAuthorityRuleConfigurationBuilder().build().getAuthorityProvider(), "");
+    }
+    
+    @Override
+    public Class<AuthorityRuleConfiguration> getTypeClass() {
+        return AuthorityRuleConfiguration.class;
+    }
+    
+    @Override
+    public String getRuleTagName() {
+        return "AUTHORITY";
+    }
+    
+    @Override
+    public int getOrder() {
+        return AuthorityOrder.ORDER;
+    }
+}
diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/metadata/AuthorityNodeConverterTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/metadata/AuthorityNodeConverterTest.java
new file mode 100644
index 00000000000..6b861643f33
--- /dev/null
+++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/metadata/AuthorityNodeConverterTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.authority.metadata;
+
+import org.apache.shardingsphere.authority.metadata.converter.AuthorityNodeConverter;
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AuthorityNodeConverterTest {
+    
+    @Test
+    void assetGetRootNode() {
+        assertThat(AuthorityNodeConverter.getRootNode(), is("authority/"));
+    }
+    
+    @Test
+    void assertGetVersion() {
+        Optional<String> actual = AuthorityNodeConverter.getVersion("/rules/authority/versions/0");
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is("0"));
+    }
+}
diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..e4ced41489d
--- /dev/null
+++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.authority.yaml.swapper;
+
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+// TODO Rename YamlAuthorityRuleConfigurationSwapperTest when metadata structure adjustment completed. #25485
+class NewYamlAuthorityRuleConfigurationSwapperTest {
+    
+    private final NewYamlAuthorityRuleConfigurationSwapper swapper = new NewYamlAuthorityRuleConfigurationSwapper();
+    
+    @Test
+    void assertSwapToDataNodes() {
+        Collection<ShardingSphereUser> users = Collections.singleton(new ShardingSphereUser("root", "", "localhost"));
+        Collection<YamlDataNode> actual = swapper.swapToDataNodes(new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null));
+        assertThat(actual.iterator().next().getKey(), is("authority/"));
+    }
+}
diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/NewDatabaseRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/NewDatabaseRulePersistService.java
index 6b1eb4775a8..2c30505c446 100644
--- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/NewDatabaseRulePersistService.java
+++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/NewDatabaseRulePersistService.java
@@ -70,10 +70,7 @@ public final class NewDatabaseRulePersistService implements NewDatabaseRuleBased
             if (Strings.isNullOrEmpty(NewDatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()))) {
                 repository.persist(NewDatabaseMetaDataNode.getDatabaseRuleActiveVersionNode(databaseName, ruleName, each.getKey()), DEFAULT_VERSION);
             }
-            List<String> versions = repository.getChildrenKeys(NewDatabaseMetaDataNode.getDatabaseRuleVersionsNode(databaseName, ruleName, each.getKey()));
-            repository.persist(NewDatabaseMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), versions.isEmpty()
-                    ? DEFAULT_VERSION
-                    : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), each.getValue());
+            repository.persist(NewDatabaseMetaDataNode.getDatabaseRuleVersionNode(databaseName, ruleName, each.getKey(), DEFAULT_VERSION), each.getValue());
         }
     }