use of herddb.network.Channel in project herddb by diennea.
the class LocalChannelTest method testServerPushesData.
@Test
public void testServerPushesData() throws Exception {
InetSocketAddress addr = new InetSocketAddress("localhost", NetworkUtils.assignFirstFreePort());
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor(addr.getHostName(), addr.getPort(), true)) {
acceptor.setEnableRealNetwork(false);
acceptor.setAcceptor((Channel channel) -> {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void requestReceived(Pdu message, Channel channel) {
try {
ByteBuf msg = buildAckResponse(message);
// send a message to the client
ByteBuf buffer = buildAckRequest(900);
Pdu response = channel.sendMessageWithPduReply(900, buffer, 10000);
assertEquals(Pdu.TYPE_ACK, response.type);
// send the response to the client
channel.sendReplyMessage(message.messageId, msg);
message.close();
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (TimeoutException ex) {
ex.printStackTrace();
}
}
@Override
public void channelClosed(Channel channel) {
}
});
return (ServerSideConnection) () -> new Random().nextLong();
});
acceptor.start();
assertNotNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
ExecutorService executor = Executors.newCachedThreadPool();
CopyOnWriteArrayList<Long> pushedMessagesFromServer = new CopyOnWriteArrayList<>();
try (Channel client = NettyConnector.connect(addr.getHostName(), addr.getPort(), true, 0, 0, new ChannelEventListener() {
@Override
public void requestReceived(Pdu pdu, Channel channel) {
pushedMessagesFromServer.add(pdu.messageId);
assertTrue(pdu.isRequest());
ByteBuf msg = buildAckResponse(pdu);
// send the response to the server
channel.sendReplyMessage(pdu.messageId, msg);
pdu.close();
}
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, null)) {
ByteBuf buffer = buildAckRequest(134);
try (Pdu result = client.sendMessageWithPduReply(134, buffer, 10000)) {
assertEquals(Pdu.TYPE_ACK, result.type);
}
assertEquals(1, pushedMessagesFromServer.size());
} finally {
executor.shutdown();
}
}
assertNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
}
use of herddb.network.Channel 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.network.Channel 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.network.Channel 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);
}
}
use of herddb.network.Channel in project herddb by diennea.
the class RoutedClientSideConnection method dumpTableSpace.
@Override
public void dumpTableSpace(String tableSpace, int fetchSize, boolean includeTransactionLog, TableSpaceDumpReceiver receiver) throws HDBException, ClientSideMetadataProviderException {
Channel channel = ensureOpen();
try {
String dumpId = this.clientId + ":" + scannerIdGenerator.incrementAndGet();
long requestId = channel.generateRequestId();
ByteBuf message = PduCodec.RequestTablespaceDump.write(requestId, tableSpace, dumpId, fetchSize, includeTransactionLog);
LOGGER.log(Level.SEVERE, "dumpTableSpace id {0} for tablespace {1}", new Object[] { dumpId, tableSpace });
dumpReceivers.put(dumpId, receiver);
try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
LOGGER.log(Level.SEVERE, "dumpTableSpace id {0} for tablespace {1}: first reply {2}", new Object[] { dumpId, tableSpace, reply });
if (reply.type == Pdu.TYPE_ERROR) {
handleGenericError(reply, 0);
} else if (reply.type != Pdu.TYPE_ACK) {
throw new HDBException(reply);
}
}
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
throw new HDBException(err);
} catch (TimeoutException err) {
throw new HDBException(err);
}
}
Aggregations