Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6774457

Browse files
akhakump911de
authored andcommitted
Disable schema metadata when performing SchemaActions.
Additionally, don't force refresh schemas if schema metadata is disabled. Closes: #990 Closes: #1253 Original pull request: #1255.
1 parent a4816da commit 6774457

File tree

4 files changed

+130
-7
lines changed

4 files changed

+130
-7
lines changed

‎spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/CqlSessionFactoryBean.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* @author John Blum
6868
* @author Mark Paluch
6969
* @author Tomasz Lelek
70+
* @author Ammar Khaku
7071
* @since 3.0
7172
*/
7273
public class CqlSessionFactoryBean
@@ -454,11 +455,20 @@ public void afterPropertiesSet() {
454455

455456
this.session = buildSession(sessionBuilder);
456457

457-
executeCql(getStartupScripts().stream(), this.session);
458-
performSchemaAction();
458+
try {
459+
SchemaRefreshUtils.withDisabledSchema(this.session, () -> {
460+
executeCql(getStartupScripts().stream(), this.session);
461+
performSchemaAction();
462+
});
463+
} catch (RuntimeException e) {
464+
throw e;
465+
} catch (Exception e) {
466+
throw new IllegalStateException("Unexpected checked exception thrown", e);
467+
}
459468

460-
this.systemSession.refreshSchema();
461-
this.session.refreshSchema();
469+
if (this.systemSession.isSchemaMetadataEnabled()) {
470+
this.systemSession.refreshSchema();
471+
}
462472
}
463473

464474
protected CqlSessionBuilder buildBuilder() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.cassandra.config;
18+
19+
import com.datastax.oss.driver.api.core.session.Session;
20+
21+
/**
22+
* Utility methods for executing schema actions with refresh disabled.
23+
*
24+
* @author Ammar Khaku
25+
*/
26+
class SchemaRefreshUtils {
27+
@FunctionalInterface
28+
interface ThrowingRunnable {
29+
void run() throws Exception;
30+
}
31+
32+
/**
33+
* Programmatically disables schema refreshes on the session and runs the provided Runnable,
34+
* taking care to restore the previous state of schema refresh config on the provided session.
35+
* Note that the session could have had schema refreshes enabled/disabled either
36+
* programmatically or via config.
37+
*/
38+
static void withDisabledSchema(Session session, ThrowingRunnable r) throws Exception {
39+
boolean schemaEnabledPreviously = session.isSchemaMetadataEnabled();
40+
session.setSchemaMetadataEnabled(false);
41+
r.run();
42+
session.setSchemaMetadataEnabled(null); // triggers schema refresh if results in true
43+
if (schemaEnabledPreviously != session.isSchemaMetadataEnabled()) {
44+
// user may have set it programmatically so set it back programmatically
45+
session.setSchemaMetadataEnabled(schemaEnabledPreviously);
46+
}
47+
}
48+
}

‎spring-data-cassandra/src/main/java/org/springframework/data/cassandra/config/SessionFactoryFactoryBean.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* keyspace before applying {@link SchemaAction schema actions} such as creating user-defined types and tables.
3838
*
3939
* @author Mark Paluch
40+
* @author Ammar Khaku
4041
* @since 3.0
4142
* @see SessionFactoryInitializer
4243
*/
@@ -132,9 +133,7 @@ public void afterPropertiesSet() throws Exception {
132133
this.keyspacePopulator.populate(getObject().getSession());
133134
}
134135

135-
performSchemaAction();
136-
137-
this.session.refreshSchema();
136+
SchemaRefreshUtils.withDisabledSchema(session, this::performSchemaAction);
138137
}
139138

140139
@Override
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.cassandra.config;
18+
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.Mock;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import com.datastax.oss.driver.api.core.session.Session;
27+
28+
/**
29+
* Test suite of unit tests testing the contract and functionality of the {@link SchemaRefreshUtils} class.
30+
*/
31+
@ExtendWith(MockitoExtension.class)
32+
class SchemaRefreshUtilsUnitTests {
33+
@Mock Session session;
34+
35+
@Test
36+
void withDisabledSchemaRevert() throws Exception {
37+
when(session.isSchemaMetadataEnabled()).thenReturn(true);
38+
SchemaRefreshUtils.withDisabledSchema(session, () -> {});
39+
verify(session).setSchemaMetadataEnabled(false);
40+
verify(session).setSchemaMetadataEnabled(null);
41+
}
42+
43+
@Test
44+
void withDisabledSchemaDisabledPreviously() throws Exception {
45+
when(session.isSchemaMetadataEnabled()).thenReturn(false);
46+
SchemaRefreshUtils.withDisabledSchema(session, () -> {});
47+
verify(session).setSchemaMetadataEnabled(false);
48+
verify(session).setSchemaMetadataEnabled(null);
49+
}
50+
51+
@Test
52+
void withDisabledSchemaDisabledProgrammaticallyPreviously() throws Exception {
53+
when(session.isSchemaMetadataEnabled()).thenReturn(false).thenReturn(true);
54+
SchemaRefreshUtils.withDisabledSchema(session, () -> {});
55+
verify(session, times(2)).setSchemaMetadataEnabled(false);
56+
verify(session).setSchemaMetadataEnabled(null);
57+
}
58+
59+
@Test
60+
void withDisabledSchemaEnabledProgrammaticallyPreviously() throws Exception {
61+
when(session.isSchemaMetadataEnabled()).thenReturn(true).thenReturn(false);
62+
SchemaRefreshUtils.withDisabledSchema(session, () -> {});
63+
verify(session).setSchemaMetadataEnabled(true);
64+
verify(session).setSchemaMetadataEnabled(null);
65+
}
66+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /