-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-27666 Fix missed SessionContext in CacheInterceptor for jdbc c… #12660
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
Open
timoninmaxim
wants to merge
1
commit into
apache:master
Choose a base branch
from
timoninmaxim:IGNITE-27666__session_ctx_jdbc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
.../ignite/internal/processors/query/calcite/jdbc/JdbcSetClientInfoCacheInterceptorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * 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.ignite.internal.processors.query.calcite.jdbc; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.Statement; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import javax.cache.Cache; | ||
| import org.apache.ignite.Ignite; | ||
| import org.apache.ignite.cache.CacheAtomicityMode; | ||
| import org.apache.ignite.cache.CacheInterceptorAdapter; | ||
| import org.apache.ignite.cache.QueryEntity; | ||
| import org.apache.ignite.cache.query.SqlFieldsQuery; | ||
| import org.apache.ignite.calcite.CalciteQueryEngineConfiguration; | ||
| import org.apache.ignite.configuration.CacheConfiguration; | ||
| import org.apache.ignite.configuration.IgniteConfiguration; | ||
| import org.apache.ignite.configuration.SqlConfiguration; | ||
| import org.apache.ignite.internal.IgniteEx; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.apache.ignite.resources.SessionContextProviderResource; | ||
| import org.apache.ignite.session.SessionContextProvider; | ||
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| import static org.junit.Assume.assumeFalse; | ||
|
|
||
| /** */ | ||
| @RunWith(Parameterized.class) | ||
| public class JdbcSetClientInfoCacheInterceptorTest extends GridCommonAbstractTest { | ||
| /** */ | ||
| private static final String SESSION_ID = "sessionId"; | ||
|
|
||
| /** */ | ||
| private static final String URL = "jdbc:ignite:thin://127.0.0.1"; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameter | ||
| public boolean runInTx; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameter(1) | ||
| public CacheAtomicityMode cacheMode; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameters(name = "runInTx={0}, mode={1}") | ||
| public static Collection<Object[]> data() { | ||
| return F.asList( | ||
| new Object[] { false, CacheAtomicityMode.TRANSACTIONAL }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. plz use GridTestUtils.cartesianProduct instead |
||
| new Object[] { false, CacheAtomicityMode.ATOMIC }, | ||
| new Object[] { true, CacheAtomicityMode.TRANSACTIONAL }, | ||
| new Object[] { true, CacheAtomicityMode.ATOMIC } | ||
| ); | ||
| } | ||
|
|
||
| /** */ | ||
| @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { | ||
| IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); | ||
|
|
||
| cfg.setSqlConfiguration(new SqlConfiguration() | ||
| .setQueryEnginesConfiguration(new CalciteQueryEngineConfiguration().setDefault(true))); | ||
|
|
||
| cfg.getTransactionConfiguration().setTxAwareQueriesEnabled(runInTx); | ||
|
|
||
| QueryEntity entity = new QueryEntity() | ||
| .setTableName("MYTABLE") | ||
| .setKeyType(Integer.class.getName()) | ||
| .setValueType(String.class.getName()) | ||
| .addQueryField("id", Integer.class.getName(), null) | ||
| .addQueryField("sessionId", String.class.getName(), null) | ||
| .setKeyFieldName("id") | ||
| .setValueFieldName("sessionId"); | ||
|
|
||
| cfg.setCacheConfiguration(new CacheConfiguration<Integer, String>() | ||
| .setAtomicityMode(cacheMode) | ||
| .setName(DEFAULT_CACHE_NAME) | ||
| .setSqlSchema("PUBLIC") | ||
| .setQueryEntities(List.of(entity)) | ||
| .setInterceptor(new SessionContextCacheInterceptor())); | ||
|
|
||
| return cfg; | ||
| } | ||
|
|
||
| /** */ | ||
| @Test | ||
| public void testInterceptInsert() throws Exception { | ||
| assumeFalse(runInTx && cacheMode == CacheAtomicityMode.ATOMIC); | ||
|
|
||
| try (Ignite ignore = startGrid(); Connection conn = DriverManager.getConnection(URL)) { | ||
| conn.setClientInfo(SESSION_ID, "42"); | ||
|
|
||
| try (Statement s = conn.createStatement()) { | ||
| assertEquals(1, s.executeUpdate("insert into PUBLIC.MYTABLE(id, sessionId) values (0, 1);")); | ||
| } | ||
|
|
||
| try (Statement s = conn.createStatement()) { | ||
| assertTrue(s.execute("select id, sessionId from PUBLIC.MYTABLE;")); | ||
|
|
||
| ResultSet rs = s.getResultSet(); | ||
| assertTrue(rs.next()); | ||
|
|
||
| assertEquals(0, rs.getInt("id")); | ||
| assertEquals("42", rs.getString("sessionId")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** */ | ||
| public static class SessionContextCacheInterceptor extends CacheInterceptorAdapter<Integer, String> { | ||
| /** */ | ||
| @SessionContextProviderResource | ||
| private SessionContextProvider sessionCtxProv; | ||
|
|
||
| /** */ | ||
| @Override public @Nullable String onBeforePut(Cache.Entry<Integer, String> entry, String newVal) { | ||
| return sessionCtxProv.getSessionContext().getAttribute(SESSION_ID); | ||
| } | ||
| } | ||
|
|
||
| /** */ | ||
| private List<List<?>> query(IgniteEx ign, String sql, Object... args) { | ||
| return ign.context().query().querySqlFields(new SqlFieldsQuery(sql).setArgs(args), false).getAll(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
| import org.apache.ignite.cache.CacheAtomicityMode; | ||
| import org.apache.ignite.cache.QueryEntity; | ||
| import org.apache.ignite.cache.query.SqlFieldsQuery; | ||
| import org.apache.ignite.cache.query.annotations.QuerySqlFunction; | ||
| import org.apache.ignite.cache.query.annotations.QuerySqlTableFunction; | ||
|
|
@@ -41,35 +43,72 @@ | |
| import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| import static org.junit.Assume.assumeFalse; | ||
|
|
||
| /** */ | ||
| @RunWith(Parameterized.class) | ||
| public class JdbcSetClientInfoTest extends GridCommonAbstractTest { | ||
| /** */ | ||
| private static final String SESSION_ID = "sessionId"; | ||
|
|
||
| /** */ | ||
| private static final String URL = "jdbc:ignite:thin://127.0.0.1"; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameter | ||
| public boolean runInTx; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameter(1) | ||
| public CacheAtomicityMode cacheMode; | ||
|
|
||
| /** */ | ||
| @Parameterized.Parameters(name = "runInTx={0}, mode={1}") | ||
| public static Collection<Object[]> data() { | ||
| return F.asList( | ||
| new Object[] { false, CacheAtomicityMode.TRANSACTIONAL }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use GridTestUtils.cartesianProduct |
||
| new Object[] { false, CacheAtomicityMode.ATOMIC }, | ||
| new Object[] { true, CacheAtomicityMode.TRANSACTIONAL }, | ||
| new Object[] { true, CacheAtomicityMode.ATOMIC } | ||
| ); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override protected IgniteConfiguration getConfiguration(String instanceName) throws Exception { | ||
| IgniteConfiguration cfg = super.getConfiguration(instanceName); | ||
|
|
||
| cfg.setSqlConfiguration(new SqlConfiguration() | ||
| .setQueryEnginesConfiguration(new CalciteQueryEngineConfiguration().setDefault(true))); | ||
|
|
||
| cfg.getTransactionConfiguration().setTxAwareQueriesEnabled(runInTx); | ||
|
|
||
| QueryEntity entity = new QueryEntity() | ||
| .setTableName("MYTABLE") | ||
| .setKeyType(Integer.class.getName()) | ||
| .setValueType(String.class.getName()) | ||
| .addQueryField("id", Integer.class.getName(), null) | ||
| .addQueryField("sessionId", String.class.getName(), null) | ||
| .setKeyFieldName("id") | ||
| .setValueFieldName("sessionId"); | ||
|
|
||
| cfg.setCacheConfiguration(new CacheConfiguration<>() | ||
| .setName(DEFAULT_CACHE_NAME) | ||
| .setAtomicityMode(cacheMode) | ||
| .setSqlSchema("PUBLIC") | ||
| .setQueryEntities(List.of(entity)) | ||
| .setSqlFunctionClasses(SessionContextFunctions.class)); | ||
|
|
||
| return cfg; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override protected void beforeTest() throws Exception { | ||
| IgniteEx ign = startGrids(3); | ||
| assumeFalse(runInTx && cacheMode == CacheAtomicityMode.ATOMIC); | ||
|
|
||
| query(ign, "create table PUBLIC.MYTABLE(id int primary key, sessionId varchar);"); | ||
| startGrids(3); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add test for sql api.