Forum: Building VoltDB Clients

Post: Are callback functions of asynchronous stored-procedure invocation thread safe?

Are callback functions of asynchronous stored-procedure invocation thread safe?
stillpedant
Jan 7, 2011
I'm building the simple application which invokes stored-procedure
asynchronously and sequentially, and want to know whether those
callback functions are thread safe or not.
For example, In the following sample code, the finale value of counter
is guaranteed to be same as the value of total i.e. invocation count
of stored-procedures?

public class Foo { private static long counter; private static class Callback implements ProcedureCallback { @Override public void clientCallback(ClientResponse response) { if (response.getStatus() == ClientResponse.SUCCESS) { this.counter++; } } } public static void main(String[] args) throws Exception { final org.voltdb.client.Client client = ClientFactory.createClient(); client.createConnection("localhost"); final int total = 10000; for (int i = 0; i < total; ++i) { client.callProcedure(callback, "Bar", ); } client.drain(); // assertEquals(this.counter, total); } }
Fix: Sample Code
stillpedant
Jan 7, 2011
Sample code above contains some mistakes. Here is the correct code.

public class Foo { private static long counter = 0; private static class Callback implements ProcedureCallback { @Override public void clientCallback(ClientResponse response) { if (response.getStatus() == ClientResponse.SUCCESS) { counter++; } } } public static void main(String[] args) throws Exception { final org.voltdb.client.Client client = ClientFactory.createClient(); client.createConnection("localhost"); final int total = 10000; for (int i = 0; i < total; ++i) { client.callProcedure(callback, "Bar", i); } client.drain(); // assertEquals(this.counter, total); } } BTW, the main point I want to know is asynchronous stored procedure invocation is implemented in what fashion (I/O multiplexing or thread).
Re: Are callback functions of asynchronous stored-procedure invo
aweisberg
Nov 7, 2011
stillpedant,
In the Java client library the callbacks are only as thread-safe as you make them. In this case you should use java.util.concurrent.AtomicLong or create a singleton instance of the callback and synchronize the method. By default callbacks will only be invoked by a single thread, but if you request a heavyweight client you may (depending on the number of hardware threads) see callbacks invoked by multiple threads. The C++ and PHP client libraries are single threaded so those callbacks don't need to be thread safe.
-Ariel
Ordering of callback functions of asynchronous stored-procedure
whtan98
May 9, 2012
Hi Ariel,
Referring to "...By default callbacks will only be invoked by a single thread, but if you request a heavyweight ..."
Does it mean it multiple async requests made from the same voltdb client (using same partition key value), also may see callbacks invoked by different threads ?
Below is my scenario: (Assume target table is partitioned by CustomerId)
-------------------------------------
for (int i = 0; i < 5; i++)
{
AsyncRequest(i, "CUST1", GetStatus(i));
}
So in the case of "heavyweight" client, the callbacks for the 5 requests may be invoked independent of each other ?
1) Thread1 => Callback0, Callback2, Callback3
2) Thread2 => Callback1,Callback4
Please advise.