Setting Query level consistency for Cassandra usin

2019-08-28 01:07发布

问题:

We are setting up consistency level, LOCAL_QUORUM as default. We are using this at the time of Cluster building in config -

Cluster.builder().addContactPoints(environment.getProperty("cassandra.contact-points").split(",")).withPort(port)
                    .withQueryOptions(
                            new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(environment.getProperty("cassandra.consistency-level")))
                                    .setSerialConsistencyLevel(ConsistencyLevel.valueOf(environment.getProperty("cassandra.consistency-level"))))
                    .withAuthProvider(new DsePlainTextAuthProvider(environment.getProperty("cassandra.username"),
                            environment.getProperty("cassandra.password")));

And in application.properties file we are providing consistency-level as -

cassandra.consistency-level=LOCAL_QUORUM

Now, we don't want to use LOCAL_QUORUM for some of the queries. For that we created custom repository as explained in Here.

We exposed a save method with WriteOptions in custom repository -

@NoRepositoryBean
public interface CustomizedUserRepository<T> {
    <S extends T> S save( S entity, ConsistencyLevel consistencyLevel);
}

And it's implementation -

public class UserRepositoryImpl implements CustomizedUserRepository<User> {

    @Autowired
    CassandraOperations cassandraOperations;

    @Override
    public <S extends User> S save( S entity, ConsistencyLevel consistencyLevel) {
        WriteOptions writeOptions = WriteOptions.builder().consistencyLevel(consistencyLevel).build();
        cassandraOperations.insert(entity, writeOptions);
        return entity;
    }
}

And calling this in service class as -

@Override
    public User createUser( User user) {
        return userRepository.save(user, ConsistencyLevel.ONE);
    }

It's working perfectly fine.

Now, question is, will this implementation overrides the default (Local quorum) for save user functionality? How would I ensure that this is working as -

  1. For saveUser() Consistency Level is ONE
  2. For other queries Consistency Level is Local quorum

We tried registering QueryLogger with the cluster to check if we can see Consistency Level information in logs but it prints queries only.