use of herddb.proto.Pdu in project herddb by diennea.
the class SimpleClientServerTest method testTimeoutDuringAuth.
@Test
public void testTimeoutDuringAuth() throws Exception {
Path baseDir = folder.newFolder().toPath();
ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
try (Server server = new Server(config) {
@Override
protected ServerSideConnectionPeer buildPeer(Channel channel) {
return new ServerSideConnectionPeer(channel, this) {
@Override
public void requestReceived(Pdu message, Channel channel) {
if (suspendProcessing.get()) {
LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
message.close();
return;
}
super.requestReceived(message, channel);
}
};
}
}) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
suspendProcessing.set(true);
try (HDBConnection connection2 = client.openConnection()) {
// auth will timeout
try {
connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1"));
fail("insertion should fail");
} catch (Exception e) {
TestUtils.assertExceptionPresentInChain(e, HDBOperationTimeoutException.class);
}
suspendProcessing.set(false);
// new connection
assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1")).updateCount);
}
}
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class TableSpaceManager method sendDumpedCommitLog.
private void sendDumpedCommitLog(List<DumpedLogEntry> txlogentries, Channel channel, String dumpId, final int timeout) throws TimeoutException, InterruptedException {
List<KeyValue> batch = new ArrayList<>();
for (DumpedLogEntry e : txlogentries) {
batch.add(new KeyValue(Bytes.from_array(e.logSequenceNumber.serialize()), Bytes.from_array(e.entryData)));
}
long id = channel.generateRequestId();
try (Pdu response_to_txlog = channel.sendMessageWithPduReply(id, PduCodec.TablespaceDumpData.write(id, tableSpaceName, dumpId, "txlog", null, 0, 0, 0, null, batch), timeout)) {
if (response_to_txlog.type != Pdu.TYPE_ACK) {
LOGGER.log(Level.SEVERE, "error response at txlog command");
}
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class Channel method sendMessageWithPduReply.
public Pdu sendMessageWithPduReply(long id, ByteBuf request, long timeout) throws InterruptedException, TimeoutException {
CompletableFuture<Pdu> resp = new CompletableFuture<>();
long _start = System.currentTimeMillis();
sendRequestWithAsyncReply(id, request, timeout, (Pdu message1, Throwable error) -> {
if (error != null) {
resp.completeExceptionally(error);
} else {
resp.complete(message1);
}
});
try {
return resp.get(timeout, TimeUnit.MILLISECONDS);
} catch (ExecutionException err) {
if (err.getCause() instanceof IOException) {
TimeoutException te = new TimeoutException("io-error while waiting for reply from " + this.getRemoteAddress() + ": " + err.getCause());
te.initCause(err.getCause());
throw te;
}
throw new RuntimeException("Error " + err + " while talking to " + this.getRemoteAddress(), err.getCause());
} catch (TimeoutException timeoutException) {
long _stop = System.currentTimeMillis();
TimeoutException err = new TimeoutException("Request timedout (" + ((_stop - _start) / 1000) + "s). Slow server " + this.getRemoteAddress() + " or internal error");
err.initCause(timeoutException);
throw err;
}
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method executeUpdateAsync.
@Override
public CompletableFuture<DMLResult> executeUpdateAsync(String tableSpace, String query, long tx, boolean returnValues, boolean usePreparedStatement, List<Object> params) {
CompletableFuture<DMLResult> res = new CompletableFuture<>();
try {
Channel channel = ensureOpen();
long requestId = channel.generateRequestId();
long statementId = usePreparedStatement ? prepareQuery(tableSpace, query) : 0;
query = statementId > 0 ? "" : query;
ByteBuf message = PduCodec.ExecuteStatement.write(requestId, tableSpace, query, tx, returnValues, statementId, params);
channel.sendRequestWithAsyncReply(requestId, message, timeout, (msg, error) -> {
if (error != null) {
res.completeExceptionally(error);
return;
}
try (Pdu reply = msg) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, statementId);
return;
} else if (reply.type != Pdu.TYPE_EXECUTE_STATEMENT_RESULT) {
throw new HDBException(reply);
}
long updateCount = PduCodec.ExecuteStatementResult.readUpdateCount(reply);
long transactionId = PduCodec.ExecuteStatementResult.readTx(reply);
boolean hasData = PduCodec.ExecuteStatementResult.hasRecord(reply);
Object key = null;
Map<RawString, Object> newvalue = null;
if (hasData) {
PduCodec.ObjectListReader parametersReader = PduCodec.ExecuteStatementResult.readRecord(reply);
newvalue = readParametersListAsMap(parametersReader);
key = newvalue.get(RAWSTRING_KEY);
}
res.complete(new DMLResult(updateCount, key, newvalue, transactionId));
} catch (HDBException | ClientSideMetadataProviderException err) {
res.completeExceptionally(err);
}
});
} catch (HDBException | ClientSideMetadataProviderException err) {
res.completeExceptionally(err);
}
return res;
}
use of herddb.proto.Pdu in project herddb by diennea.
the class RoutedClientSideConnection method executeGet.
@Override
public GetResult executeGet(String tableSpace, String query, long tx, boolean usePreparedStatement, List<Object> params) throws HDBException, ClientSideMetadataProviderException {
Channel channel = ensureOpen();
try {
long requestId = channel.generateRequestId();
long statementId = usePreparedStatement ? prepareQuery(tableSpace, query) : 0;
query = statementId > 0 ? "" : query;
ByteBuf message = PduCodec.ExecuteStatement.write(requestId, tableSpace, query, tx, true, statementId, params);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, statementId);
} else if (reply.type != Pdu.TYPE_EXECUTE_STATEMENT_RESULT) {
throw new HDBException(reply);
}
long updateCount = PduCodec.ExecuteStatementResult.readUpdateCount(reply);
long transactionId = PduCodec.ExecuteStatementResult.readTx(reply);
boolean hasData = PduCodec.ExecuteStatementResult.hasRecord(reply);
Map<RawString, Object> data = null;
if (hasData) {
PduCodec.ObjectListReader parametersReader = PduCodec.ExecuteStatementResult.readRecord(reply);
data = readParametersListAsMap(parametersReader);
}
if (updateCount <= 0) {
return new GetResult(null, transactionId);
} else {
return new GetResult(data, transactionId);
}
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
Aggregations