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

ConsistencyLevel for batchOps does not work #1192

Open
wtetsu opened this issue Nov 15, 2021 · 1 comment
Open

ConsistencyLevel for batchOps does not work #1192

wtetsu opened this issue Nov 15, 2021 · 1 comment
Labels
type: enhancement A general enhancement

Comments

@wtetsu
Copy link

wtetsu commented Nov 15, 2021

Hello,
I noticed consistencyLevel cannot be set when using batchOps.

  • spring-data-cassandra 3.2.2 and 3.3.0
  • Cassandra 4.0.1 (3 nodes, replication factor:2)

For example, try executing this code that uses batchOps.

Right after this batch process finished, run a query(ONE) to read the deleted data(*1).
Even though ALL is specified in queries in batchOps, the read query sometimes returns the data that was deleted a while ago.

In a few seconds, the same read request always returns empty.
It looks like "ALL" for writing doesn't work.

cassandraTemplate.batchOps()
    .insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
    .delete(..., DeleteOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build()) // (*1)
    .insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
    .execute();

Next, I tried running this code that set ConsistencyLevel.ALL via BatchStatementBuilder.
it worked as expected.
Queries to read the deleted data(*1) never find data even if right after this batch process finished.

var batchOperations = cassandraTemplate.batchOps();
BatchStatementBuilder batch = (BatchStatementBuilder) getField(batchOperations, "batch");
batch.setConsistencyLevel(ConsistencyLevel.ALL);

batchOperations.insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
               .delete(..., DeleteOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build()) // (*1)
               .insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
               .execute();


//private static Object getField(Object obj, String fieldName) {
//  Field privateStringField;
//  try {
//    privateStringField = obj.getClass().getDeclaredField(fieldName);
//    privateStringField.setAccessible(true);
//    return privateStringField.get(obj);
//  } catch (NoSuchFieldException | IllegalAccessException e) {
//    throw new RuntimeException("Failed to getField", e);
//  }
//}

However, the code above accesses the private "batch" field, so I don't think this is "legal" code.

Although StatementBuilder in DataStax Java Driver has public setConsistencyLevel, I cannot call it via Spring Data. Is there any reason it is not exposed to Spring Data users?


Another trial is here.
wtetsu@d806d32

I tried to implement batchOps that can accept ConsistencyLevel.
It worked as expected.

cassandraTemplate.batchOps(ConsistencyLevel.ALL)
    .insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
    .delete(..., DeleteOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build()) // (*1)
    .insert(..., InsertOptions.builder().consistencyLevel(ConsistencyLevel.ALL).build())
    .execute();

Here are my questions:

  • Is there a formal way to specify consistencyLevel when using batchOps?
  • If no, is it possible to expand batchOps features in order to realize it?

Thank you.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 15, 2021
@mp911de
Copy link
Member

mp911de commented Nov 15, 2021

I'm not sure how much consistency levels apply to individual statements within a batch. Likely, not at all. However, we're properly propagating the consistency level into each statement. The batch statement is executed through CqlTemplate.queryForResultSet(…) so you could hook in there to update the consistency level.

We could consider applying generally QueryOptions to BatchStatementBuilder for setting options on the batch itself.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants