diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 0db9cb1685ab7..48cb213ca46b8 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/pull/34156) 1. SQL Binder: Support sql bind for select with current select projection reference - [#34151](https://github.com/apache/shardingsphere/pull/34151) 1. SQL Binder: Support alter table, drop table sql bind and add test case - [#34154](https://github.com/apache/shardingsphere/pull/34154) 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