From b076c8919da7902eb6e2bec017965874eba86ddd Mon Sep 17 00:00:00 2001 From: youjie_li Date: Thu, 26 Dec 2024 12:08:17 +0800 Subject: [PATCH 1/2] Add SQLStatementParserCacheHook to Provide Extension Point for Application SQL Parse Cache Warm-up , Referred to as Preheat --- .../parser/cache/SQLStatementCacheLoader.java | 16 ++++- .../hook/SPISQLStatementParserCacheHook.java | 49 +++++++++++++++ .../hook/SQLStatementParserCacheHook.java | 49 +++++++++++++++ .../SPISQLStatementParserCacheHookTest.java | 55 +++++++++++++++++ .../SQLStatementParserCacheHookFixture.java | 60 +++++++++++++++++++ ...ser.cache.hook.SQLStatementParserCacheHook | 18 ++++++ 6 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/hook/SPISQLStatementParserCacheHook.java create mode 100644 infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHook.java create mode 100644 infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SPISQLStatementParserCacheHookTest.java create mode 100644 infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHookFixture.java create mode 100644 infra/parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.cache.hook.SQLStatementParserCacheHook diff --git a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheLoader.java b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheLoader.java index 3df17d6031e15..ec85205176fb9 100644 --- a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheLoader.java +++ b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/SQLStatementCacheLoader.java @@ -19,6 +19,7 @@ import com.github.benmanes.caffeine.cache.CacheLoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.parser.cache.hook.SPISQLStatementParserCacheHook; import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserExecutor; import org.apache.shardingsphere.sql.parser.api.CacheOption; import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; @@ -33,12 +34,23 @@ public final class SQLStatementCacheLoader implements CacheLoader sqlStatementParserCacheHooks = ShardingSphereServiceLoader.getServiceInstances(SQLStatementParserCacheHook.class); + + @Override + public void start(final String sql) { + for (SQLStatementParserCacheHook each : sqlStatementParserCacheHooks) { + each.start(sql); + } + } + + @Override + public void finishSuccess(final String sql, final SQLStatement sqlStatement) { + for (SQLStatementParserCacheHook each : sqlStatementParserCacheHooks) { + each.finishSuccess(sql, sqlStatement); + } + } + + @Override + public void finishFailure(final String sql, final Exception cause) { + for (SQLStatementParserCacheHook each : sqlStatementParserCacheHooks) { + each.finishFailure(sql, cause); + } + } +} diff --git a/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHook.java b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHook.java new file mode 100644 index 0000000000000..0a8592cf1d5d3 --- /dev/null +++ b/infra/parser/src/main/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHook.java @@ -0,0 +1,49 @@ +/* + * 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.parser.cache.hook; + +import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; + +/** + * SQL Statement Parser Cache hook. + */ +public interface SQLStatementParserCacheHook { + + /** + * Handle when parse started. + * + * @param sql SQL to be parsed + */ + void start(String sql); + + /** + * Handle when parse finished success. + * + * @param sql SQL to be parsed + * @param sqlStatement sql statement + */ + void finishSuccess(String sql, SQLStatement sqlStatement); + + /** + * Handle when parse finished failure. + * + * @param sql SQL to be parsed + * @param cause failure cause + */ + void finishFailure(String sql, Exception cause); +} diff --git a/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SPISQLStatementParserCacheHookTest.java b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SPISQLStatementParserCacheHookTest.java new file mode 100644 index 0000000000000..385e24c71f93d --- /dev/null +++ b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SPISQLStatementParserCacheHookTest.java @@ -0,0 +1,55 @@ +/* + * 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.parser.cache.hook; + +import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SPISQLStatementParserCacheHookTest { + + public static final String SQL = "SELECT 1"; + + private SPISQLStatementParserCacheHook spisqlStatementParserCacheHook; + + @BeforeEach + void setUp() { + SQLStatementParserCacheHookFixture.clearActions(); + spisqlStatementParserCacheHook = new SPISQLStatementParserCacheHook(); + } + + @Test + void assertStart() { + spisqlStatementParserCacheHook.start(SQL); + assertTrue(SQLStatementParserCacheHookFixture.containsAction("start")); + } + + @Test + void assertFinishSuccess() { + spisqlStatementParserCacheHook.finishSuccess(SQL, new MySQLSelectStatement()); + assertTrue(SQLStatementParserCacheHookFixture.containsAction("finishSuccess")); + } + + @Test + void assertFinishFailure() { + spisqlStatementParserCacheHook.finishFailure(SQL, new Exception()); + assertTrue(SQLStatementParserCacheHookFixture.containsAction("finishFailure")); + } +} diff --git a/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHookFixture.java b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHookFixture.java new file mode 100644 index 0000000000000..688a7f2a5401f --- /dev/null +++ b/infra/parser/src/test/java/org/apache/shardingsphere/infra/parser/cache/hook/SQLStatementParserCacheHookFixture.java @@ -0,0 +1,60 @@ +/* + * 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.parser.cache.hook; + +import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; + +import java.util.Collection; +import java.util.LinkedList; + +public final class SQLStatementParserCacheHookFixture implements SQLStatementParserCacheHook { + + private static final Collection ACTIONS = new LinkedList<>(); + + @Override + public void start(final String sql) { + ACTIONS.add("start"); + } + + @Override + public void finishSuccess(final String sql, final SQLStatement statement) { + ACTIONS.add("finishSuccess"); + } + + @Override + public void finishFailure(final String sql, final Exception cause) { + ACTIONS.add("finishFailure"); + } + + /** + * Contains action or not. + * + * @param action action + * @return contains action or not + */ + public static boolean containsAction(final String action) { + return ACTIONS.contains(action); + } + + /** + * Clear actions. + */ + public static void clearActions() { + ACTIONS.clear(); + } +} diff --git a/infra/parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.cache.hook.SQLStatementParserCacheHook b/infra/parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.cache.hook.SQLStatementParserCacheHook new file mode 100644 index 0000000000000..d3a81208df627 --- /dev/null +++ b/infra/parser/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.parser.cache.hook.SQLStatementParserCacheHook @@ -0,0 +1,18 @@ +# +# 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.cache.hook.SQLStatementParserCacheHookFixture From af3663ed5cbfc3a603c4be4dd8662460993e63c1 Mon Sep 17 00:00:00 2001 From: youjie_li Date: Thu, 26 Dec 2024 12:33:04 +0800 Subject: [PATCH 2/2] Add SQLStatementParserCacheHook to Provide Extension Point for Application SQL Parse Cache Warm-up , Referred to as Preheat --- RELEASE-NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 37fad67b25538..9aa215f26b04d 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -39,6 +39,7 @@ 1. SQL Parser: Support MySQL update with statement parse - [#34126](https://github.com/apache/shardingsphere/pull/34126) 1. SQL Binder: Remove TablesContext#findTableNames method and implement select order by, group by bind logic - [#34123](https://github.com/apache/shardingsphere/pull/34123) 1. SQL Binder: Support select with statement sql bind and add bind test case - [#34141](https://github.com/apache/shardingsphere/pull/34141) +1. SQL Parser: Add SQLStatementParserCacheHook to Provide Extension Point for Application SQL Parse Cache Warm-up , Referred to as Preheat - [#34155](https://github.com/apache/shardingsphere/issues/34155) ### Bug Fixes