DATACASS-328 - Revise QueryOptions and WriteOptions.

Deprecate our org.springframework.cassandra.core.ConsistencyLevel enum. Having an own consistency level type leads to confusion and it's always behind the driver. We don't want to maintain that type, so we decided to deprecate the own type and use the driver consistency levels.

We allow now the use of the driver retry policies aside of our consistency level enumeration. For most cases, our enumeration is the simpler approach. Some retry policies (IdempotenceAwareRetryPolicy, LoggingRetryPolicies) require further configuration and cannot be applied with just using a static enum value. We now support ReadTimeout, FetchSize, and Tracing via QueryOptions and WriteOptions and provide builders for QueryOptions and WriteOptions.

Original pull request: #81.
This commit is contained in:
Mark Paluch
2016-07-22 16:45:58 +02:00
committed by John Blum
parent 43ab34ac54
commit fd8e430592
12 changed files with 1029 additions and 31 deletions

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Rule;
@@ -27,6 +28,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cassandra.support.CassandraExceptionTranslator;
import org.springframework.cassandra.support.exception.CassandraReadTimeoutException;
@@ -36,31 +38,44 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.exceptions.DriverException;
import com.datastax.driver.core.exceptions.ReadTimeoutException;
import com.datastax.driver.core.policies.FallthroughRetryPolicy;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.querybuilder.Update;
import com.datastax.driver.core.querybuilder.Using;
/**
* The CqlTemplateUnitTests class is a test suite of test cases testing the contract and functionality of the
* {@link CqlTemplate} class.
*
* @author John Blum
* @author Mark Paluch
*/
// TODO: add many more unit tests until SUT test coverage is 100%!
@RunWith(MockitoJUnitRunner.class)
@SuppressWarnings("unchecked")
public class CqlTemplateUnitTests {
@Rule
public ExpectedException exception = ExpectedException.none();
@Rule public ExpectedException exception = ExpectedException.none();
private CqlTemplate template;
@Mock
private Session mockSession;
@Mock private Session mockSession;
@Mock private PreparedStatement mockPreparedStatement;
@Mock private Statement mockStatement;
@Mock private Insert mockInsert;
@Mock private Update mockUpdate;
@Before
public void setup() {
@@ -71,7 +86,8 @@ public class CqlTemplateUnitTests {
@Test
public void doExecuteInSessionCallbackIsCalled() {
String result = template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
@Override
public String doInSession(Session session) throws DataAccessException {
session.execute("test");
return "test";
}
@@ -91,7 +107,8 @@ public class CqlTemplateUnitTests {
exception.expectCause(org.hamcrest.Matchers.isA(ReadTimeoutException.class));
template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
@Override
public String doInSession(Session session) throws DataAccessException {
throw new ReadTimeoutException(ConsistencyLevel.ALL, 0, 1, true);
}
});
@@ -107,7 +124,8 @@ public class CqlTemplateUnitTests {
exception.expectMessage(containsString("test"));
template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
@Override
public String doInSession(Session session) throws DataAccessException {
throw new DriverException("test");
}
});
@@ -123,7 +141,8 @@ public class CqlTemplateUnitTests {
exception.expectMessage(containsString("test"));
template.doExecute(new SessionCallback<String>() {
@Override public String doInSession(Session session) throws DataAccessException {
@Override
public String doInSession(Session session) throws DataAccessException {
throw new Error("test");
}
});
@@ -406,4 +425,144 @@ public class CqlTemplateUnitTests {
template.processOne(null, String.class);
}
/**
* @see DATACASS-202
*/
@Test
public void addPreparedStatementOptionsShouldAddDriverQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder() //
.consistencyLevel(ConsistencyLevel.EACH_QUORUM) //
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
.build();
template.addPreparedStatementOptions(mockPreparedStatement, queryOptions);
verify(mockPreparedStatement).setConsistencyLevel(ConsistencyLevel.EACH_QUORUM);
verify(mockPreparedStatement).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
}
/**
* @see DATACASS-202
*/
@Test
public void addPreparedStatementOptionsShouldAddOurQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder() //
.retryPolicy(RetryPolicy.FALLTHROUGH).build();
queryOptions.setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel.LOCAL_QUOROM);
template.addPreparedStatementOptions(mockPreparedStatement, queryOptions);
verify(mockPreparedStatement).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
verify(mockPreparedStatement).setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
}
/**
* @see DATACASS-202
*/
@Test
public void addStatementQueryOptionsShouldAddDriverQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder().consistencyLevel(ConsistencyLevel.EACH_QUORUM) //
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
.build();
template.addQueryOptions(mockStatement, queryOptions);
verify(mockStatement).setConsistencyLevel(ConsistencyLevel.EACH_QUORUM);
verify(mockStatement).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
}
/**
* @see DATACASS-202
*/
@Test
public void addStatementQueryOptionsShouldAddOurQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder() //
.retryPolicy(RetryPolicy.FALLTHROUGH) //
.build();
queryOptions.setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel.LOCAL_QUOROM);
template.addQueryOptions(mockStatement, queryOptions);
verify(mockStatement).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
verify(mockStatement).setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
}
/**
* @see DATACASS-202
*/
@Test
public void addStatementQueryOptionsShouldNotAddOptions() {
QueryOptions queryOptions = QueryOptions.builder().build();
template.addQueryOptions(mockStatement, queryOptions);
verifyZeroInteractions(mockStatement);
}
/**
* @see DATACASS-202
*/
@Test
public void addStatementQueryOptionsShouldAddGenericQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder() //
.readTimeout(1, TimeUnit.MINUTES) //
.fetchSize(10) //
.withTracing() //
.build();
template.addQueryOptions(mockStatement, queryOptions);
verify(mockStatement).setReadTimeoutMillis(60 * 1000);
verify(mockStatement).setFetchSize(10);
verify(mockStatement).enableTracing();
}
/**
* @see DATACASS-202
*/
@Test
public void addInsertWriteOptionsShouldAddDriverQueryOptions() {
WriteOptions writeOptions = WriteOptions.builder() //
.consistencyLevel(ConsistencyLevel.EACH_QUORUM) //
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
.readTimeout(10) //
.ttl(10) //
.build();
template.addWriteOptions(mockInsert, writeOptions);
verify(mockInsert).setConsistencyLevel(ConsistencyLevel.EACH_QUORUM);
verify(mockInsert).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
verify(mockInsert).setReadTimeoutMillis(10);
verify(mockInsert).using(Mockito.any(Using.class));
}
/**
* @see DATACASS-202
*/
@Test
public void addUpdateWriteOptionsShouldAddDriverQueryOptions() {
WriteOptions writeOptions = WriteOptions.builder() //
.consistencyLevel(ConsistencyLevel.EACH_QUORUM) //
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
.ttl(10) //
.tracing(false)
.build();
template.addWriteOptions(mockUpdate, writeOptions);
verify(mockUpdate).setConsistencyLevel(ConsistencyLevel.EACH_QUORUM);
verify(mockUpdate).setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
verify(mockUpdate).using(Mockito.any(Using.class));
verify(mockUpdate).disableTracing();
}
}

View File

@@ -0,0 +1,153 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.springframework.cassandra.core;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.policies.DefaultRetryPolicy;
import com.datastax.driver.core.policies.FallthroughRetryPolicy;
import com.datastax.driver.core.policies.LoggingRetryPolicy;
/**
* Unit tests for {@link QueryOptions}.
*
* @author Mark Paluch
*/
public class QueryOptionsUnitTests {
/**
* @see DATACASS-202
*/
@Test
public void buildQueryOptions() {
QueryOptions queryOptions = QueryOptions.builder() //
.consistencyLevel(ConsistencyLevel.ANY) //
.retryPolicy(RetryPolicy.DEFAULT) //
.readTimeout(1, TimeUnit.SECONDS)//
.fetchSize(10)//
.tracing(true)//
.build(); //
assertThat((Class) queryOptions.getClass(), is(equalTo((Class) QueryOptions.class)));
assertThat(queryOptions.getRetryPolicy(), is(RetryPolicy.DEFAULT));
assertThat(queryOptions.getConsistencyLevel(), is(nullValue()));
assertThat(queryOptions.getDriverConsistencyLevel(), is(ConsistencyLevel.ANY));
assertThat(queryOptions.getReadTimeout(), is(1000L));
assertThat(queryOptions.getFetchSize(), is(10));
assertThat(queryOptions.getTracing(), is(true));
}
/**
* @see DATACASS-202
*/
@Test
public void buildQueryOptionsWithDriverRetryPolicy() {
QueryOptions writeOptions = QueryOptions.builder() //
.retryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE)) //
.build(); //
assertThat(writeOptions.getRetryPolicy(), is(nullValue()));
assertThat(writeOptions.getDriverRetryPolicy(), is(instanceOf(LoggingRetryPolicy.class)));
}
/**
* @see DATACASS-202
*/
@Test
public void buildQueryOptionsWithRetryPolicy() {
QueryOptions writeOptions = QueryOptions.builder() //
.retryPolicy(RetryPolicy.DOWNGRADING_CONSISTENCY) //
.build(); //
assertThat(writeOptions.getRetryPolicy(), is(RetryPolicy.DOWNGRADING_CONSISTENCY));
assertThat(writeOptions.getDriverRetryPolicy(), is(nullValue()));
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void builderShouldRejectSettingOurAndDriverRetryPolicy() {
QueryOptions.builder() //
.retryPolicy(RetryPolicy.DEFAULT).retryPolicy(FallthroughRetryPolicy.INSTANCE);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void builderShouldRejectSettingDriverAndOurRetryPolicy() {
QueryOptions.builder() //
.retryPolicy(FallthroughRetryPolicy.INSTANCE)//
.retryPolicy(RetryPolicy.DEFAULT);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void shouldRejectSettingOurAndDriverRetryPolicy() {
QueryOptions queryOptions = new QueryOptions();
queryOptions.setRetryPolicy(RetryPolicy.DEFAULT);
queryOptions.setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void shouldRejectSettingDriverAndOurRetryPolicy() {
QueryOptions queryOptions = new QueryOptions();
queryOptions.setRetryPolicy(FallthroughRetryPolicy.INSTANCE);
queryOptions.setRetryPolicy(RetryPolicy.DEFAULT);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void shouldRejectSettingOurAndDriverConsistencyLevel() {
QueryOptions queryOptions = new QueryOptions();
queryOptions.setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel.ANY);
queryOptions.setConsistencyLevel(ConsistencyLevel.ANY);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void shouldRejectSettingDriverAndOurConsistencyLevel() {
QueryOptions queryOptions = new QueryOptions();
queryOptions.setConsistencyLevel(ConsistencyLevel.ANY);
queryOptions.setConsistencyLevel(org.springframework.cassandra.core.ConsistencyLevel.ANY);
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed 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.springframework.cassandra.core;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.datastax.driver.core.policies.FallthroughRetryPolicy;
/**
* Unit tests for {@link WriteOptions}.
*
* @author Mark Paluch
*/
public class WriteOptionsUnitTests {
/**
* @see DATACASS-202
*/
@Test
public void buildWriteOptions() {
WriteOptions writeOptions = WriteOptions.builder() //
.consistencyLevel(com.datastax.driver.core.ConsistencyLevel.ANY) //
.ttl(123) //
.retryPolicy(RetryPolicy.DEFAULT) //
.readTimeout(1)//
.fetchSize(10)//
.withTracing()//
.build(); //
assertThat(writeOptions.getTtl(), is(123));
assertThat(writeOptions.getRetryPolicy(), is(RetryPolicy.DEFAULT));
assertThat(writeOptions.getConsistencyLevel(), is(nullValue()));
assertThat(writeOptions.getDriverConsistencyLevel(), is(com.datastax.driver.core.ConsistencyLevel.ANY));
assertThat(writeOptions.getReadTimeout(), is(1L));
assertThat(writeOptions.getFetchSize(), is(10));
assertThat(writeOptions.getTracing(), is(true));
}
/**
* @see DATACASS-202
*/
@Test
public void buildReadTimeoutOptionsWriteOptions() {
WriteOptions writeOptions = WriteOptions.builder() //
.readTimeout(1, TimeUnit.MINUTES)//
.build(); //
assertThat(writeOptions.getReadTimeout(), is(60L * 1000L));
assertThat(writeOptions.getFetchSize(), is(nullValue()));
assertThat(writeOptions.getTracing(), is(nullValue()));
}
/**
* @see DATACASS-202
*/
@Test
public void buildQueryOptionsWithDriverRetryPolicy() {
QueryOptions writeOptions = QueryOptions.builder() //
.retryPolicy(FallthroughRetryPolicy.INSTANCE) //
.build(); //
assertThat(writeOptions.getRetryPolicy(), is(nullValue()));
assertThat(writeOptions.getDriverRetryPolicy(),
is(equalTo((com.datastax.driver.core.policies.RetryPolicy) FallthroughRetryPolicy.INSTANCE)));
}
/**
* @see DATACASS-202
*/
@Test
public void buildQueryOptionsWithRetryPolicy() {
QueryOptions writeOptions = QueryOptions.builder() //
.retryPolicy(RetryPolicy.DOWNGRADING_CONSISTENCY) //
.build(); //
assertThat(writeOptions.getRetryPolicy(), is(RetryPolicy.DOWNGRADING_CONSISTENCY));
assertThat(writeOptions.getDriverRetryPolicy(), is(nullValue()));
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void builderShouldRejectSettingOurAndDriverRetryPolicy() {
WriteOptions.builder() //
.retryPolicy(RetryPolicy.DEFAULT).retryPolicy(FallthroughRetryPolicy.INSTANCE);
}
/**
* @see DATACASS-202
*/
@Test(expected = IllegalStateException.class)
public void builderShouldRejectSettingDriverAndOurRetryPolicy() {
WriteOptions.builder() //
.retryPolicy(FallthroughRetryPolicy.INSTANCE)//
.retryPolicy(RetryPolicy.DEFAULT);
}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.cassandra.test.integration.core;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Collection;
@@ -1087,7 +1088,27 @@ public class CqlOperationsIntegrationTests extends AbstractKeyspaceCreatingInteg
Truncate truncate = QueryBuilder.truncate(tableName);
cqlTemplate.execute(truncate);
}
/**
* @see DATACASS-202
*/
@Test
public void queryShouldApplyFetchSize() {
insertTestObjectArray();
final String cql = "select * from book";
ResultSet oneByOneResultSet = cqlTemplate.query(cql, QueryOptions.builder().fetchSize(1).build());
assertThat(oneByOneResultSet.isFullyFetched(), is(false));
assertThat(oneByOneResultSet.getAvailableWithoutFetching(), is(1));
ResultSet fullResultSet = cqlTemplate.query(cql, QueryOptions.builder().fetchSize(10).build());
assertThat(fullResultSet.isFullyFetched(), is(true));
assertThat(fullResultSet.getAvailableWithoutFetching(), is(4));
}
/**

View File

@@ -269,7 +269,7 @@ public class AsynchronousCqlOperationsIntegrationTests extends AbstractKeyspaceC
new AsynchronousQueryListenerTestTemplate() {
@Override
void doAsyncQuery(Book b, QueryListener listener) {
cqlOperations.queryAsynchronously(cql(b), listener, new QueryOptions(cl, RetryPolicy.LOGGING));
cqlOperations.queryAsynchronously(cql(b), listener, new QueryOptions(cl, RetryPolicy.DEFAULT));
}
}.test();
}