Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQLStatementParserCacheHook to Provide Extension Point for Application SQL Parse Cache Warm-up , Referred to as Preheat #34156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,12 +34,23 @@ public final class SQLStatementCacheLoader implements CacheLoader<String, SQLSta
private final SQLStatementParserExecutor sqlStatementParserExecutor;

public SQLStatementCacheLoader(final DatabaseType databaseType, final CacheOption parseTreeCacheOption) {
sqlStatementParserExecutor = new SQLStatementParserExecutor(databaseType, parseTreeCacheOption);
this.sqlStatementParserExecutor = new SQLStatementParserExecutor(databaseType, parseTreeCacheOption);
}

@ParametersAreNonnullByDefault
@Override
public SQLStatement load(final String sql) {
return sqlStatementParserExecutor.parse(sql);
SPISQLStatementParserCacheHook spisqlStatementParserCacheHook = new SPISQLStatementParserCacheHook();
spisqlStatementParserCacheHook.start(sql);
try {
SQLStatement sqlStatement = sqlStatementParserExecutor.parse(sql);
spisqlStatementParserCacheHook.finishSuccess(sql, sqlStatement);
return sqlStatement;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
spisqlStatementParserCacheHook.finishFailure(sql, ex);
throw ex;
}
}
}
Original file line number Diff line number Diff line change
@@ -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.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;

import java.util.Collection;

public class SPISQLStatementParserCacheHook implements SQLStatementParserCacheHook {

private final Collection<SQLStatementParserCacheHook> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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
Loading