This is an automated email from the ASF dual-hosted git repository.

tuichenchuxin 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 d42ca1d  Fix system table metadata not loaded exception when start 
packaged proxy (#16489)
d42ca1d is described below

commit d42ca1d450f8a11e4d99b63f61060b1c6a567986
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Mar 30 14:27:27 2022 +0800

    Fix system table metadata not loaded exception when start packaged proxy 
(#16489)
---
 .../schema/builder/SystemSchemaBuilder.java        | 37 +++++------
 .../schema/builder/SystemSchemaBuilderRule.java    | 75 ++++++++++++++++++++++
 .../builder/SystemSchemaBuilderRuleTest.java       | 45 +++++++++++++
 .../schema/builder/SystemSchemaBuilderTest.java    | 50 +++++++++++++++
 4 files changed, 187 insertions(+), 20 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilder.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilder.java
index 02b01f9..7c359e2 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilder.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilder.java
@@ -19,22 +19,20 @@ package 
org.apache.shardingsphere.infra.metadata.schema.builder;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlTableMetaData;
 import 
org.apache.shardingsphere.infra.yaml.schema.swapper.TableMetaDataYamlSwapper;
+import org.yaml.snakeyaml.Yaml;
 
-import java.io.File;
-import java.net.URL;
-import java.util.Arrays;
+import java.io.InputStream;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.Map;
 
 /**
@@ -50,16 +48,15 @@ public final class SystemSchemaBuilder {
      * @param databaseType database type
      * @return ShardingSphere schema map
      */
-    @SneakyThrows
     public static Map<String, ShardingSphereSchema> build(final String 
databaseName, final DatabaseType databaseType) {
         Map<String, ShardingSphereSchema> result = new 
LinkedHashMap<>(databaseType.getSystemSchemas().size(), 1);
         TableMetaDataYamlSwapper swapper = new TableMetaDataYamlSwapper();
         for (String each : getSystemSchemas(databaseName, databaseType)) {
-            Collection<File> schemaFiles = getSchemaFiles(each, databaseType);
-            if (schemaFiles.isEmpty()) {
+            Collection<InputStream> schemaStreams = getSchemaStreams(each, 
databaseType);
+            if (schemaStreams.isEmpty()) {
                 continue;
             }
-            result.put(each, createSchema(schemaFiles, swapper));
+            result.put(each, createSchema(schemaStreams, swapper));
         }
         return result;
     }
@@ -69,20 +66,20 @@ public final class SystemSchemaBuilder {
         return 
databaseType.getSystemDatabaseSchemaMap().getOrDefault(databaseName, 
Collections.emptyList());
     }
     
-    private static Collection<File> getSchemaFiles(final String schemaName, 
final DatabaseType databaseType) {
-        URL url = 
SystemSchemaBuilder.class.getClassLoader().getResource("schema/" + 
databaseType.getName().toLowerCase() + "/" + schemaName);
-        if (null == url) {
-            return Collections.emptyList();
+    private static Collection<InputStream> getSchemaStreams(final String 
schemaName, final DatabaseType databaseType) {
+        String databaseTypeName = databaseType.getName().toLowerCase();
+        SystemSchemaBuilderRule builderRule = 
SystemSchemaBuilderRule.valueOf(databaseTypeName, schemaName);
+        Collection<InputStream> result = new LinkedList<>();
+        for (String each : builderRule.getTables()) {
+            
result.add(SystemSchemaBuilder.class.getClassLoader().getResourceAsStream("schema/"
 + databaseTypeName + "/" + schemaName + "/" + each + ".yaml"));
         }
-        File[] files = new File(url.getFile()).listFiles();
-        return null == files ? Collections.emptyList() : Arrays.asList(files);
+        return result;
     }
     
-    @SneakyThrows
-    private static ShardingSphereSchema createSchema(final Collection<File> 
schemaFiles, final TableMetaDataYamlSwapper swapper) {
-        Map<String, TableMetaData> tables = new 
LinkedHashMap<>(schemaFiles.size(), 1);
-        for (File file : schemaFiles) {
-            YamlTableMetaData metaData = YamlEngine.unmarshal(file, 
YamlTableMetaData.class);
+    private static ShardingSphereSchema createSchema(final 
Collection<InputStream> schemaStreams, final TableMetaDataYamlSwapper swapper) {
+        Map<String, TableMetaData> tables = new 
LinkedHashMap<>(schemaStreams.size(), 1);
+        for (InputStream each : schemaStreams) {
+            YamlTableMetaData metaData = new Yaml().loadAs(each, 
YamlTableMetaData.class);
             tables.put(metaData.getName(), swapper.swapToObject(metaData));
         }
         return new ShardingSphereSchema(tables);
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRule.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRule.java
new file mode 100644
index 0000000..f81458b
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRule.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.infra.metadata.schema.builder;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * System schema builder rule.
+ */
+@RequiredArgsConstructor
+@Getter
+public enum SystemSchemaBuilderRule {
+    
+    MYSQL_INFORMATION_SCHEMA("mysql.information_schema", 
Arrays.asList("columns", "tables", "views")),
+    
+    MYSQL_MYSQL("mysql.mysql", Collections.singletonList("db")),
+    
+    MYSQL_PERFORMANCE_SCHEMA("mysql.performance_schema", 
Collections.singletonList("accounts")),
+    
+    MYSQL_SYS("mysql.sys", Collections.singletonList("sys")),
+    
+    POSTGRESQL_INFORMATION_SCHEMA("postgresql.information_schema", 
Arrays.asList("columns", "tables", "views")),
+    
+    POSTGRESQL_PG_CATALOG("postgresql.pg_catalog", 
Arrays.asList("pg_database", "pg_tablespace"));
+    
+    private static final Map<String, SystemSchemaBuilderRule> 
SCHEMA_PATH_SYSTEM_SCHEMA_BUILDER_RULE_MAP = new HashMap<>();
+    
+    private final String schemaPath;
+    
+    private final Collection<String> tables;
+    
+    static {
+        for (SystemSchemaBuilderRule each : values()) {
+            
SCHEMA_PATH_SYSTEM_SCHEMA_BUILDER_RULE_MAP.put(each.getSchemaPath(), each);
+        }
+    }
+
+    /**
+     * Value of builder rule.
+     * 
+     * @param databaseTypeName database type name
+     * @param schema schema
+     * @return builder rule
+     */
+    public static SystemSchemaBuilderRule valueOf(final String 
databaseTypeName, final String schema) {
+        String schemaPath = databaseTypeName + "." + schema;
+        SystemSchemaBuilderRule result = 
SCHEMA_PATH_SYSTEM_SCHEMA_BUILDER_RULE_MAP.get(schemaPath);
+        if (null == result) {
+            throw new IllegalArgumentException(String.format("Can not find 
builder rule: `%s`", schemaPath));
+        }
+        return result;
+    }
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRuleTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRuleTest.java
new file mode 100644
index 0000000..147e82c
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderRuleTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.infra.metadata.schema.builder;
+
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class SystemSchemaBuilderRuleTest {
+    
+    @Test
+    public void assertValueOfSchemaPathSuccess() {
+        String databaseName = new MySQLDatabaseType().getName().toLowerCase();
+        String schemaName = "information_schema";
+        SystemSchemaBuilderRule actual = 
SystemSchemaBuilderRule.valueOf(databaseName, schemaName);
+        assertThat(actual, 
is(SystemSchemaBuilderRule.MYSQL_INFORMATION_SCHEMA));
+        assertThat(actual.getTables(), is(Arrays.asList("columns", "tables", 
"views")));
+    }
+    
+    @Test(expected = IllegalArgumentException.class)
+    public void assertValueOfSchemaPathFailure() {
+        String databaseName = new MySQLDatabaseType().getName().toLowerCase();
+        String schemaName = "test";
+        SystemSchemaBuilderRule.valueOf(databaseName, schemaName);
+    }
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderTest.java
new file mode 100644
index 0000000..d576128
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SystemSchemaBuilderTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.infra.metadata.schema.builder;
+
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import 
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class SystemSchemaBuilderTest {
+    
+    @Test
+    public void assertBuildForMySQL() {
+        Map<String, ShardingSphereSchema> actual = 
SystemSchemaBuilder.build("information_schema", new MySQLDatabaseType());
+        assertThat(actual.size(), is(1));
+        assertTrue(actual.containsKey("information_schema"));
+        assertThat(actual.get("information_schema").getTables().size(), is(3));
+    }
+    
+    @Test
+    public void assertBuildForPostgreSQL() {
+        Map<String, ShardingSphereSchema> actual = 
SystemSchemaBuilder.build("sharding_db", new PostgreSQLDatabaseType());
+        assertThat(actual.size(), is(2));
+        assertTrue(actual.containsKey("information_schema"));
+        assertTrue(actual.containsKey("pg_catalog"));
+        assertThat(actual.get("information_schema").getTables().size(), is(3));
+        assertThat(actual.get("pg_catalog").getTables().size(), is(2));
+    }
+}

Reply via email to