Skip to content

Commit e211b95

Browse files
committed
Make driver recognize Cluster request timeout
1 parent e950084 commit e211b95

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.springframework.data.cassandra;
2+
3+
import org.springframework.dao.QueryTimeoutException;
4+
5+
/**
6+
* This exception is thrown when driver have timed out during any interation with cassandra coordinator node
7+
*
8+
* @author Mikhail Polivakha
9+
*/
10+
public class CassandraDriverTimeOutException extends QueryTimeoutException {
11+
12+
public CassandraDriverTimeOutException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/CassandraExceptionTranslator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323

2424
import org.springframework.dao.DataAccessException;
2525
import org.springframework.dao.DataAccessResourceFailureException;
26+
import org.springframework.dao.QueryTimeoutException;
2627
import org.springframework.dao.TransientDataAccessResourceException;
2728
import org.springframework.dao.support.PersistenceExceptionTranslator;
2829
import org.springframework.data.cassandra.CassandraAuthenticationException;
2930
import org.springframework.data.cassandra.CassandraConnectionFailureException;
31+
import org.springframework.data.cassandra.CassandraDriverTimeOutException;
3032
import org.springframework.data.cassandra.CassandraInsufficientReplicasAvailableException;
3133
import org.springframework.data.cassandra.CassandraInvalidConfigurationInQueryException;
3234
import org.springframework.data.cassandra.CassandraInvalidQueryException;
@@ -43,6 +45,7 @@
4345

4446
import com.datastax.oss.driver.api.core.AllNodesFailedException;
4547
import com.datastax.oss.driver.api.core.DriverException;
48+
import com.datastax.oss.driver.api.core.DriverTimeoutException;
4649
import com.datastax.oss.driver.api.core.auth.AuthenticationException;
4750
import com.datastax.oss.driver.api.core.metadata.Node;
4851
import com.datastax.oss.driver.api.core.servererrors.AlreadyExistsException;
@@ -70,6 +73,7 @@
7073
* @author Alex Shvid
7174
* @author Matthew T. Adams
7275
* @author Mark Paluch
76+
* @author Mikhail Polivakha
7377
*/
7478
@SuppressWarnings("unchecked")
7579
public class CassandraExceptionTranslator implements CqlExceptionTranslator {
@@ -105,6 +109,10 @@ public DataAccessException translate(@Nullable String task, @Nullable String cql
105109
exception);
106110
}
107111

112+
if (exception instanceof DriverTimeoutException driverTimeoutException) {
113+
return new CassandraDriverTimeOutException(driverTimeoutException.getMessage(), driverTimeoutException);
114+
}
115+
108116
if (exception instanceof ReadTimeoutException) {
109117
return new CassandraReadTimeoutException(((ReadTimeoutException) exception).wasDataPresent(), message, exception);
110118
}
@@ -178,7 +186,6 @@ public DataAccessException translate(@Nullable String task, @Nullable String cql
178186
// unknown or unhandled exception
179187
return new CassandraUncategorizedException(message, exception);
180188
}
181-
182189
return null;
183190
}
184191

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/cql/CassandraExceptionTranslatorUnitTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.dao.TransientDataAccessResourceException;
2929
import org.springframework.data.cassandra.CassandraAuthenticationException;
3030
import org.springframework.data.cassandra.CassandraConnectionFailureException;
31+
import org.springframework.data.cassandra.CassandraDriverTimeOutException;
3132
import org.springframework.data.cassandra.CassandraInsufficientReplicasAvailableException;
3233
import org.springframework.data.cassandra.CassandraInvalidConfigurationInQueryException;
3334
import org.springframework.data.cassandra.CassandraInvalidQueryException;
@@ -40,6 +41,7 @@
4041
import org.springframework.data.cassandra.CassandraWriteTimeoutException;
4142

4243
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
44+
import com.datastax.oss.driver.api.core.DriverTimeoutException;
4345
import com.datastax.oss.driver.api.core.NoNodeAvailableException;
4446
import com.datastax.oss.driver.api.core.ProtocolVersion;
4547
import com.datastax.oss.driver.api.core.UnsupportedProtocolVersionException;
@@ -72,6 +74,7 @@
7274
*
7375
* @author Matthew T. Adams
7476
* @author Mark Paluch
77+
* @author Mikhail Polivakha
7578
*/
7679
class CassandraExceptionTranslatorUnitTests {
7780

@@ -117,6 +120,14 @@ void shouldTranslateInvalidConfigurationInQueryException() {
117120
.hasMessageStartingWith("message").hasCauseInstanceOf(InvalidConfigurationInQueryException.class);
118121
}
119122

123+
@Test
124+
void shouldRecognizeDriverTimeoutException() {
125+
DataAccessException dataAccessException = sut.translateExceptionIfPossible(new DriverTimeoutException("message"));
126+
127+
assertThat(dataAccessException).isInstanceOf(CassandraDriverTimeOutException.class).hasMessageStartingWith("message")
128+
.hasCauseInstanceOf(DriverTimeoutException.class);
129+
}
130+
120131
@Test // DATACASS-402
121132
void shouldTranslateUnauthorizedException() {
122133

0 commit comments

Comments
 (0)