public void testRemoteIdle() throws Exception {
Client client = this.getClient();
RegressionSuiteUtil.initializeTM1Database(this.getCatalogContext(), client);
final int sleepTime = 10000; // ms
final ClientResponse dtxnResponse[] = new ClientResponse[1];
final AtomicBoolean latch = new AtomicBoolean(true);
final ProcedureCallback callback = new ProcedureCallback() {
@Override
public void clientCallback(ClientResponse clientResponse) {
// System.err.println("DISTRUBTED RESULT " + clientResponse);
dtxnResponse[0] = clientResponse;
latch.set(false);
}
};
// We're going to first execute a long running and slow distributed transaction
// on the first partition. It will sleep for 10 seconds
String procName = RemoteIdle.class.getSimpleName();
Object params[] = { 0, sleepTime };
client.callProcedure(callback, procName, params);
long start = System.currentTimeMillis();
// While we're waiting for that to come back, we're going to fire off
// a bunch of single-partition txns that should all be executed
// speculatively at the other partition
TM1Client.Transaction txn = Transaction.GET_SUBSCRIBER_DATA;
params = new Object[]{ 1 };
ClientResponse cresponse = null;
int specexec_ctr = 0;
while (latch.get()) {
// Just sleep for a little bit so that we don't blast the cluster
ThreadUtil.sleep(500);
// System.err.println("Requesting " + txn + " for speculative execution");
try {
cresponse = client.callProcedure(txn.callName, params);
assertEquals(Status.OK, cresponse.getStatus());
} catch (ProcCallException ex) {
cresponse = ex.getClientResponse();
assertEquals(cresponse.toString(), Status.ABORT_USER, cresponse.getStatus());
}
// System.err.println(cresponse.toString());
// System.err.println();
// We'll only check the txns half way through the dtxns expected
// sleep time
long elapsed = System.currentTimeMillis() - start;
assert(elapsed <= sleepTime*1.25);
if (elapsed < sleepTime/2) {
assertEquals(cresponse.toString(), latch.get(), cresponse.isSpeculative());
System.err.println(cresponse.getDebug());
}
if (cresponse.isSpeculative()) specexec_ctr++;
} // WHILE
assert(specexec_ctr > 0);
cresponse = dtxnResponse[0];
assertNotNull(cresponse);
assertTrue(cresponse.hasDebug());
assertFalse(cresponse.isSinglePartition());
assertFalse(cresponse.isSpeculative());
}