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 2020/10/30 05:44:16 UTC

[shardingsphere] branch master updated: Use one parsingHook for SQL and DistSQL parser to sent tracing event (#7968)

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

zhangyonglun 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 1fa89ae  Use one parsingHook for SQL and DistSQL parser to sent tracing event (#7968)
1fa89ae is described below

commit 1fa89ae28c0ac760fda76071f40d09992c291f97
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Oct 30 13:43:50 2020 +0800

    Use one parsingHook for SQL and DistSQL parser to sent tracing event (#7968)
    
    * Use one parsingHook for SQL and DistSQL parser to sent tracing event
    
    * Move ParsingHook to infra-parser module
---
 .../parser/ShardingSphereSQLParserEngine.java      | 34 ++++++++++++++++++---
 .../infra}/parser/hook/ParsingHook.java            |  2 +-
 .../infra}/parser/hook/ParsingHookRegistry.java    |  2 +-
 .../infra/parser/sql/SQLStatementParserEngine.java | 23 --------------
 .../parser/hook/ParsingHookRegistryTest.java       |  4 +--
 .../parser/hook/fixture/ParsingHookFixture.java    |  4 +--
 ...he.shardingsphere.infra.parser.hook.ParsingHook | 35 ++++++++++++++++++++++
 .../opentracing/hook/OpenTracingParsingHook.java   |  2 +-
 ...e.shardingsphere.infra.parser.hook.ParsingHook} |  0
 .../hook/OpenTracingParsingHookTest.java           |  4 +--
 ...ache.shardingsphere.sql.parser.hook.ParsingHook | 18 -----------
 11 files changed, 74 insertions(+), 54 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
index 9ee56f5..e20bef9 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/ShardingSphereSQLParserEngine.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.infra.parser;
 import org.apache.shardingsphere.distsql.parser.DistSQLStatementParserEngine;
 import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
 import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngineFactory;
+import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
+import org.apache.shardingsphere.infra.parser.hook.ParsingHookRegistry;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 /**
@@ -31,11 +33,19 @@ public final class ShardingSphereSQLParserEngine {
     
     private final DistSQLStatementParserEngine distSQLStatementParserEngine;
     
+    private final ParsingHookRegistry parsingHookRegistry;
+    
     public ShardingSphereSQLParserEngine(final String databaseTypeName) {
         sqlStatementParserEngine = SQLStatementParserEngineFactory.getSQLStatementParserEngine(databaseTypeName);
         distSQLStatementParserEngine = new DistSQLStatementParserEngine();
+        parsingHookRegistry = ParsingHookRegistry.getInstance();
     }
     
+    /*
+     * To make sure SkyWalking will be available at the next release of ShardingSphere, a new plugin should be provided to SkyWalking project if this API changed.
+     *
+     * @see <a href="https://github.com/apache/skywalking/blob/master/docs/en/guides/Java-Plugin-Development-Guide.md#user-content-plugin-development-guide">Plugin Development Guide</a>
+     */
     /**
      * Parse to SQL statement.
      *
@@ -43,15 +53,31 @@ public final class ShardingSphereSQLParserEngine {
      * @param useCache whether use cache
      * @return SQL statement
      */
+    @SuppressWarnings("OverlyBroadCatchBlock")
     public SQLStatement parse(final String sql, final boolean useCache) {
-        SQLStatement result;
+        parsingHookRegistry.start(sql);
         try {
-            result = sqlStatementParserEngine.parse(sql, useCache);
+            SQLStatement result = parse0(sql, useCache);
+            parsingHookRegistry.finishSuccess(result);
+            return result;
             // CHECKSTYLE:OFF
+            // TODO check whether throw SQLParsingException only
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
-            result = distSQLStatementParserEngine.parse(sql);
+            parsingHookRegistry.finishFailure(ex);
+            throw ex;
+        }
+    }
+    
+    private SQLStatement parse0(final String sql, final boolean useCache) {
+        try {
+            return sqlStatementParserEngine.parse(sql, useCache);
+        } catch (final SQLParsingException originalEx) {
+            try {
+                return distSQLStatementParserEngine.parse(sql);
+            } catch (final SQLParsingException ignored) {
+                throw originalEx;
+            }
         }
-        return result;
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHook.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHook.java
similarity index 96%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHook.java
rename to shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHook.java
index 1fe4c44..b2f9028 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHook.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHook.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.hook;
+package org.apache.shardingsphere.infra.parser.hook;
 
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistry.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistry.java
similarity index 97%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistry.java
rename to shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistry.java
index bf33e87..4a600ef 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistry.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistry.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.hook;
+package org.apache.shardingsphere.infra.parser.hook;
 
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
index 2fe0193..5e830fd 100644
--- a/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserEngine.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.infra.parser.sql;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.sql.parser.api.SQLParserEngine;
 import org.apache.shardingsphere.sql.parser.cache.SQLParsedResultCache;
-import org.apache.shardingsphere.sql.parser.hook.ParsingHookRegistry;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Optional;
@@ -35,13 +34,6 @@ public final class SQLStatementParserEngine {
     
     private final SQLParsedResultCache<SQLStatement> cache = new SQLParsedResultCache<>();
     
-    private final ParsingHookRegistry parsingHookRegistry = ParsingHookRegistry.getInstance();
-    
-    /*
-     * To make sure SkyWalking will be available at the next release of ShardingSphere, a new plugin should be provided to SkyWalking project if this API changed.
-     *
-     * @see <a href="https://github.com/apache/skywalking/blob/master/docs/en/guides/Java-Plugin-Development-Guide.md#user-content-plugin-development-guide">Plugin Development Guide</a>
-     */
     /**
      * Parse to SQL statement.
      *
@@ -49,22 +41,7 @@ public final class SQLStatementParserEngine {
      * @param useCache whether use cache
      * @return SQL statement
      */
-    @SuppressWarnings("OverlyBroadCatchBlock")
     public SQLStatement parse(final String sql, final boolean useCache) {
-        parsingHookRegistry.start(sql);
-        try {
-            SQLStatement result = parse0(sql, useCache);
-            parsingHookRegistry.finishSuccess(result);
-            return result;
-            // CHECKSTYLE:OFF
-        } catch (final Exception ex) {
-            // CHECKSTYLE:ON
-            parsingHookRegistry.finishFailure(ex);
-            throw ex;
-        }
-    }
-    
-    private SQLStatement parse0(final String sql, final boolean useCache) {
         if (!useCache) {
             return SQLParserEngine.parse(databaseTypeName, sql, false, "STATEMENT");
         }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistryTest.java b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistryTest.java
similarity index 92%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistryTest.java
rename to shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistryTest.java
index 93881d6..4d90d97 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/ParsingHookRegistryTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/ParsingHookRegistryTest.java
@@ -15,9 +15,9 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.hook;
+package org.apache.shardingsphere.infra.parser.hook;
 
-import org.apache.shardingsphere.sql.parser.hook.fixture.ParsingHookFixture;
+import org.apache.shardingsphere.infra.parser.hook.fixture.ParsingHookFixture;
 import org.junit.Before;
 import org.junit.Test;
 
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/fixture/ParsingHookFixture.java b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/fixture/ParsingHookFixture.java
similarity index 93%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/fixture/ParsingHookFixture.java
rename to shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/fixture/ParsingHookFixture.java
index db17996..1f0fa36 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/hook/fixture/ParsingHookFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/test/java/org/apache/shardingsphere/infra/parser/hook/fixture/ParsingHookFixture.java
@@ -15,9 +15,9 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.hook.fixture;
+package org.apache.shardingsphere.infra.parser.hook.fixture;
 
-import org.apache.shardingsphere.sql.parser.hook.ParsingHook;
+import org.apache.shardingsphere.infra.parser.hook.ParsingHook;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Collection;
diff --git a/shardingsphere-infra/shardingsphere-infra-parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.hook.ParsingHook b/shardingsphere-infra/shardingsphere-infra-parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.hook.ParsingHook
new file mode 100644
index 0000000..37ed2b0
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.hook.ParsingHook
@@ -0,0 +1,35 @@
+#
+# 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.
+#
+
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.parser.hook.fixture.ParsingHookFixture
diff --git a/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHook.java b/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHook.java
index 904a8ac..21aa4f6 100644
--- a/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHook.java
+++ b/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHook.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.tracing.opentracing.hook;
 
 import io.opentracing.Span;
 import io.opentracing.tag.Tags;
-import org.apache.shardingsphere.sql.parser.hook.ParsingHook;
+import org.apache.shardingsphere.infra.parser.hook.ParsingHook;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.tracing.opentracing.OpenTracingTracer;
 import org.apache.shardingsphere.tracing.opentracing.constant.ShardingTags;
diff --git a/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/resources/META-INF/services/org.apache.shardingsphere.sql.parser.hook.ParsingHook b/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.parser.hook.ParsingHook
similarity index 100%
rename from shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/resources/META-INF/services/org.apache.shardingsphere.sql.parser.hook.ParsingHook
rename to shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.parser.hook.ParsingHook
diff --git a/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/test/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHookTest.java b/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/test/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHookTest.java
index 3d15690..d69a06a 100644
--- a/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/test/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHookTest.java
+++ b/shardingsphere-observability/shardingsphere-tracing/shardingsphere-tracing-opentracing/src/test/java/org/apache/shardingsphere/tracing/opentracing/hook/OpenTracingParsingHookTest.java
@@ -21,8 +21,8 @@ import io.opentracing.mock.MockSpan;
 import io.opentracing.tag.Tags;
 import org.apache.shardingsphere.tracing.opentracing.constant.ShardingTags;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.sql.parser.hook.ParsingHook;
-import org.apache.shardingsphere.sql.parser.hook.ParsingHookRegistry;
+import org.apache.shardingsphere.infra.parser.hook.ParsingHook;
+import org.apache.shardingsphere.infra.parser.hook.ParsingHookRegistry;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.junit.BeforeClass;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.hook.ParsingHook b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.hook.ParsingHook
deleted file mode 100644
index d6f5ebc..0000000
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.hook.ParsingHook
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.
-#
-
-org.apache.shardingsphere.sql.parser.hook.fixture.ParsingHookFixture