This is an automated email from the ASF dual-hosted git repository.
panjuan 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 3a40a51 add global rule configurations event,listener, persist into
and load from RegistryCenter (#10309)
3a40a51 is described below
commit 3a40a51dae49a4f7babf88c7c61b419ac79517a2
Author: huanghao495430759 <[email protected]>
AuthorDate: Wed May 12 05:52:24 2021 -0500
add global rule configurations event,listener, persist into and load from
RegistryCenter (#10309)
* add GLOBAL_RULE_NODE in RegistryCenterNode.
add global rule configurations persist and load in RegistryCenter.
add global rule changed event and listener.
* code format
Co-authored-by: huanghao <[email protected]>
---
.../governance/core/registry/RegistryCenter.java | 19 ++++++++
.../core/registry/RegistryCenterNode.java | 15 +++++-
.../rule/GlobalRuleConfigurationsChangedEvent.java | 37 ++++++++++++++
.../listener/impl/GlobalRuleChangedListener.java | 56 ++++++++++++++++++++++
.../core/registry/RegistryCenterNodeTest.java | 13 +++--
.../core/registry/RegistryCenterTest.java | 23 +++++++++
.../impl/GlobalRuleChangedListenerTest.java | 54 +++++++++++++++++++++
.../src/test/resources/yaml/authority-rule.yaml | 24 ++++++++++
.../yaml/registryCenter/data-global-rule.yaml | 24 ++++++++++
9 files changed, 259 insertions(+), 6 deletions(-)
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
index 243c9ea..932da85 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenter.java
@@ -221,6 +221,16 @@ public final class RegistryCenter {
return !Strings.isNullOrEmpty(repository.get(node.getPropsPath()));
}
+ private void persistGlobalRuleConfigurations(final
Collection<RuleConfiguration> globalRuleConfigs, final boolean isOverwrite) {
+ if (!globalRuleConfigs.isEmpty() && (isOverwrite ||
!hasGlobalRuleConfigurations())) {
+ repository.persist(node.getGlobalRuleNode(),
YamlEngine.marshal(new
YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(globalRuleConfigs)));
+ }
+ }
+
+ private boolean hasGlobalRuleConfigurations() {
+ return
!Strings.isNullOrEmpty(repository.get(node.getGlobalRuleNode()));
+ }
+
private void persistSchemaName(final String schemaName) {
String schemaNames = repository.get(node.getMetadataNodePath());
if (Strings.isNullOrEmpty(schemaNames)) {
@@ -275,6 +285,15 @@ public final class RegistryCenter {
}
/**
+ * Load global rule configurations.
+ *
+ * @return global rule configurations
+ */
+ public Collection<RuleConfiguration> loadGlobalRuleConfigurations() {
+ return hasGlobalRuleConfigurations() ?
YamlConfigurationConverter.convertRuleConfigurations(repository.get(node.getGlobalRuleNode()))
: Collections.emptyList();
+ }
+
+ /**
* Get all schema names.
*
* @return all schema names
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNode.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNode.java
index fe7b162..c3e8fd0 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNode.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNode.java
@@ -56,7 +56,9 @@ public final class RegistryCenterNode {
private static final String SCHEMA_NODE = "schema";
private static final String USERS_NODE = "users";
-
+
+ private static final String GLOBAL_RULE_NODE = "rule";
+
private static final String PRIVILEGE_NODE = "privilegenode";
private static final String PROPS_NODE = "props";
@@ -254,7 +256,16 @@ public final class RegistryCenterNode {
public String getUsersNode() {
return getFullPath(USERS_NODE);
}
-
+
+ /**
+ * Get global rule node path.
+ *
+ * @return global rule node path
+ */
+ public String getGlobalRuleNode() {
+ return getFullPath(GLOBAL_RULE_NODE);
+ }
+
/**
* Get privilege node path.
*
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/event/rule/GlobalRuleConfigurationsChangedEvent.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/event/rule/GlobalRuleConfigurationsChangedEvent.java
new file mode 100644
index 0000000..c04b9d8
--- /dev/null
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/event/rule/GlobalRuleConfigurationsChangedEvent.java
@@ -0,0 +1,37 @@
+/*
+ * 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.registry.listener.event.rule;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.GovernanceEvent;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+
+import java.util.Collection;
+
+/**
+ * Global rule configurations event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class GlobalRuleConfigurationsChangedEvent implements
GovernanceEvent {
+
+ private final String schemaName;
+
+ private final Collection<RuleConfiguration> ruleConfigurations;
+}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListener.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListener.java
new file mode 100644
index 0000000..470e481
--- /dev/null
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListener.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.registry.listener.impl;
+
+import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.governance.core.registry.RegistryCenterNode;
+import
org.apache.shardingsphere.governance.core.registry.listener.PostGovernanceRepositoryEventListener;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.GovernanceEvent;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.GlobalRuleConfigurationsChangedEvent;
+import
org.apache.shardingsphere.governance.core.yaml.config.YamlRuleConfigurationWrap;
+import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
+import
org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+/**
+ * Global rule changed listener.
+ */
+public final class GlobalRuleChangedListener extends
PostGovernanceRepositoryEventListener<GovernanceEvent> {
+
+ public GlobalRuleChangedListener(final RegistryRepository
registryRepository) {
+ super(registryRepository, Collections.singleton(new
RegistryCenterNode().getGlobalRuleNode()));
+ }
+
+ @Override
+ protected Optional<GovernanceEvent> createEvent(final DataChangedEvent
event) {
+ return Optional.of(new GlobalRuleConfigurationsChangedEvent("",
getGlobalRuleConfigurations(event)));
+ }
+
+ private Collection<RuleConfiguration> getGlobalRuleConfigurations(final
DataChangedEvent event) {
+ Collection<YamlRuleConfiguration> globalRuleConfigs =
YamlEngine.unmarshal(event.getValue(),
YamlRuleConfigurationWrap.class).getRules();
+ Preconditions.checkState(!globalRuleConfigs.isEmpty(), "No available
global rule to load for governance.");
+ return new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(globalRuleConfigs);
+ }
+}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeTest.java
index 8e64b9d..319fd6c 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeTest.java
@@ -86,22 +86,27 @@ public final class RegistryCenterNodeTest {
public void assertGetRulePath() {
assertThat(registryCenterNode.getRulePath(DefaultSchema.LOGIC_NAME),
is("/metadata/logic_db/rule"));
}
-
+
@Test
public void assertGetUsersNodePath() {
assertThat(registryCenterNode.getUsersNode(), is("/users"));
}
-
+
+ @Test
+ public void assertGetGlobalRuleNodePath() {
+ assertThat(registryCenterNode.getGlobalRuleNode(), is("/rule"));
+ }
+
@Test
public void assertGetPropsPath() {
assertThat(registryCenterNode.getPropsPath(), is("/props"));
}
-
+
@Test
public void assertGetSchemaName() {
assertThat(registryCenterNode.getSchemaName("/metadata/logic_db/rule"),
is(DefaultSchema.LOGIC_NAME));
}
-
+
@Test
public void assertGetAllSchemaConfigPaths() {
Collection<String> actual =
registryCenterNode.getAllSchemaConfigPaths(Collections.singletonList(DefaultSchema.LOGIC_NAME));
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
index ecfe006..ef7c1fa 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.governance.core.registry;
import lombok.SneakyThrows;
+import
org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.governance.core.registry.listener.event.datasource.DataSourceAddedEvent;
@@ -63,6 +64,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
@@ -99,6 +101,8 @@ public final class RegistryCenterTest {
private static final String USERS_YAML =
"yaml/registryCenter/data-users.yaml";
+ private static final String GLOBAL_RULE_YAML =
"yaml/registryCenter/data-global-rule.yaml";
+
private static final String PROPS_YAML =
ConfigurationPropertyKey.SQL_SHOW.getKey() + ": false\n";
private static final String DATA_SOURCE_YAML_WITH_CONNECTION_INIT_SQL =
"yaml/registryCenter/data-source-init-sql.yaml";
@@ -491,6 +495,25 @@ public final class RegistryCenterTest {
}
@Test
+ public void assertLoadGlobalRuleConfigurations() {
+
when(registryRepository.get("/rule")).thenReturn(readYAML(GLOBAL_RULE_YAML));
+ RegistryCenter registryCenter = new RegistryCenter(registryRepository);
+ Collection<RuleConfiguration> globalRuleConfigs =
registryCenter.loadGlobalRuleConfigurations();
+ assertTrue(!globalRuleConfigs.isEmpty());
+ Collection<ShardingSphereUser> users =
globalRuleConfigs.stream().filter(each -> each instanceof
AuthorityRuleConfiguration)
+ .flatMap(each -> ((AuthorityRuleConfiguration)
each).getUsers().stream()).collect(Collectors.toList());
+ Optional<ShardingSphereUser> user = users.stream().filter(each ->
each.getGrantee().equals(new Grantee("root", ""))).findFirst();
+ assertTrue(user.isPresent());
+ assertThat(user.get().getPassword(), is("root"));
+ Collection<ShardingSphereAlgorithmConfiguration> providers =
globalRuleConfigs.stream()
+ .filter(each -> each instanceof AuthorityRuleConfiguration &&
Objects.nonNull(((AuthorityRuleConfiguration) each).getProvider()))
+ .map(each -> ((AuthorityRuleConfiguration)
each).getProvider()).collect(Collectors.toList());
+ assertTrue(!providers.isEmpty());
+ Optional<ShardingSphereAlgorithmConfiguration> nativeProvider =
providers.stream().filter(each -> "NATIVE".equals(each.getType())).findFirst();
+ assertTrue(nativeProvider.isPresent());
+ }
+
+ @Test
public void assertGetAllSchemaNames() {
when(registryRepository.get("/metadata")).thenReturn("sharding_db,replica_query_db");
RegistryCenter registryCenter = new RegistryCenter(registryRepository);
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListenerTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListenerTest.java
new file mode 100644
index 0000000..965a416
--- /dev/null
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/GlobalRuleChangedListenerTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.registry.listener.impl;
+
+import
org.apache.shardingsphere.governance.core.registry.listener.event.GovernanceEvent;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.GlobalRuleConfigurationsChangedEvent;
+import
org.apache.shardingsphere.governance.core.registry.listener.metadata.MetaDataListenerTest;
+import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
+import
org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
+import
org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent.Type;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class GlobalRuleChangedListenerTest extends MetaDataListenerTest {
+
+ private GlobalRuleChangedListener globalRuleChangedListener;
+
+ @Mock
+ private RegistryRepository registryRepository;
+
+ @Before
+ public void setUp() {
+ globalRuleChangedListener = new
GlobalRuleChangedListener(registryRepository);
+ }
+
+ @Test
+ public void assertCreateEvent() {
+ Optional<GovernanceEvent> event =
globalRuleChangedListener.createEvent(new DataChangedEvent("rule",
readYAML("yaml/authority-rule.yaml"), Type.UPDATED));
+ assertTrue(event.isPresent());
+ assertThat(event.get(),
instanceOf(GlobalRuleConfigurationsChangedEvent.class));
+ }
+}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/authority-rule.yaml
b/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/authority-rule.yaml
new file mode 100644
index 0000000..2f4b8a9
--- /dev/null
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/authority-rule.yaml
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+rules:
+ - !AUTHORITY
+ users:
+ - root@%:root
+ - sharding@:sharding
+ provider:
+ type: NATIVE
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/registryCenter/data-global-rule.yaml
b/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/registryCenter/data-global-rule.yaml
new file mode 100644
index 0000000..2f4b8a9
--- /dev/null
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/resources/yaml/registryCenter/data-global-rule.yaml
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+rules:
+ - !AUTHORITY
+ users:
+ - root@%:root
+ - sharding@:sharding
+ provider:
+ type: NATIVE