Oktay
Oktay

Reputation: 138

Are Cassandra triggers Asynchronous?

I am testing cassandra trigger and implemented the ITrigger interface. As on the ticket https://issues.apache.org/jira/browse/CASSANDRA-1311 it seems cassandra trigger should be async, means after a client made an insert, client should not wait for the trigger to be executed.

But I have added a sleep like 15 seconds in the Trigger and insert operation failed because of timeout. So it seems it is waiting the trigger finishes its job.

Is there a way to make trigger to work asynch? (it should be already async according to pages I have read)

Upvotes: 1

Views: 485

Answers (1)

Ashraful Islam
Ashraful Islam

Reputation: 12830

No, By default Cassandra wait for trigger execution.

But you can change that using execute query with QueryProcessor and run into separate thread. Note that if you do this and trigger execution failed you will not be able to know that the trigger query failed from your main query.

Example for Cassandra 2.x :

@Override
public Collection<Mutation> augment(ByteBuffer key, ColumnFamily update) {
     new Thread(new Runnable() {
        @Override
        public void run() {
            //build your own query from the ByteBuffer key and ColumnFamily  update
            Insert insert = QueryBuilder.insertInto("test_keyspace.test_table").value("id", 1).value("data", "Test");

            //execute query  
            QueryProcessor.process(insert.toString(), ConsistencyLevel.LOCAL_QUORUM);
        }
    }).start();
    return Collections.EMPTY_LIST;
}

Upvotes: 2

Related Questions