Search in sources :

Example 11 with Pdu

use of herddb.proto.Pdu in project herddb by diennea.

the class RoutedClientSideConnection method executeUpdate.

@Override
public DMLResult executeUpdate(String tableSpace, String query, long tx, boolean returnValues, 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, returnValues, 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);
            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);
            }
            return new DMLResult(updateCount, key, newvalue, transactionId);
        }
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new HDBException(err);
    } catch (TimeoutException err) {
        throw new HDBException(err);
    }
}
Also used : Pdu(herddb.proto.Pdu) RawString(herddb.utils.RawString) Channel(herddb.network.Channel) ByteBuf(io.netty.buffer.ByteBuf) PduCodec(herddb.proto.PduCodec) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with Pdu

use of herddb.proto.Pdu in project herddb by diennea.

the class RoutedClientSideConnection method commitTransaction.

@Override
public void commitTransaction(String tableSpace, long tx) throws HDBException, ClientSideMetadataProviderException {
    Channel channel = ensureOpen();
    try {
        long requestId = channel.generateRequestId();
        ByteBuf message = PduCodec.TxCommand.write(requestId, PduCodec.TxCommand.TX_COMMAND_COMMIT_TRANSACTION, tx, tableSpace);
        try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
            if (reply.type == Pdu.TYPE_ERROR) {
                handleGenericError(reply, 0);
                // not possible
                return;
            } else if (reply.type != Pdu.TYPE_TX_COMMAND_RESULT) {
                throw new HDBException(reply);
            }
        }
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new HDBException(err);
    } catch (TimeoutException err) {
        throw new HDBException(err);
    }
}
Also used : Pdu(herddb.proto.Pdu) Channel(herddb.network.Channel) ByteBuf(io.netty.buffer.ByteBuf) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with Pdu

use of herddb.proto.Pdu in project herddb by diennea.

the class LocalChannelTest method test.

@Test
public void test() 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) {
                    ByteBuf msg = buildAckResponse(message);
                    channel.sendReplyMessage(message.messageId, msg);
                    message.close();
                }

                @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();
        try (Channel client = NettyConnector.connect(addr.getHostName(), addr.getPort(), true, 0, 0, new ChannelEventListener() {

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, null)) {
            for (int i = 0; i < 100; i++) {
                ByteBuf buffer = buildAckRequest(i);
                try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
                    assertEquals(Pdu.TYPE_ACK, result.type);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
    assertNull(LocalServerRegistry.getLocalServer(NetworkUtils.getAddress(addr), addr.getPort()));
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) Pdu(herddb.proto.Pdu) Random(java.util.Random) InetSocketAddress(java.net.InetSocketAddress) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 14 with Pdu

use of herddb.proto.Pdu in project herddb by diennea.

the class ProtocolMessageDecoder method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    if (LOGGER.isLoggable(Level.FINEST)) {
        StringBuilder dumper = new StringBuilder();
        ByteBufUtil.appendPrettyHexDump(dumper, in);
        LOGGER.log(Level.FINEST, "Received from {}: {}", new Object[] { ctx.channel(), dumper });
    }
    try {
        Pdu pdu = PduCodec.decodePdu(in);
        ctx.fireChannelRead(pdu);
    } catch (Throwable err) {
        LOGGER.log(Level.SEVERE, "Error decoding PDU", err);
        ReferenceCountUtil.safeRelease(msg);
    }
}
Also used : Pdu(herddb.proto.Pdu) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with Pdu

use of herddb.proto.Pdu in project herddb by diennea.

the class ChannelBenchTest method test.

@Test
public void test() throws Exception {
    try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), false)) {
        acceptor.setAcceptor((Channel channel) -> {
            channel.setMessagesReceiver(new ChannelEventListener() {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    ByteBuf msg = buildAckResponse(message);
                    channel.sendReplyMessage(message.messageId, msg);
                    message.close();
                }

                @Override
                public void channelClosed(Channel channel) {
                }
            });
            return (ServerSideConnection) () -> new Random().nextLong();
        });
        acceptor.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        try (Channel client = NettyConnector.connect(acceptor.getHost(), acceptor.getPort(), acceptor.isSsl(), 0, 0, new ChannelEventListener() {

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
            }
        }, executor, new NioEventLoopGroup(10, executor))) {
            for (int i = 0; i < 100; i++) {
                ByteBuf buffer = buildAckRequest(i);
                try (Pdu result = client.sendMessageWithPduReply(i, buffer, 10000)) {
                    assertEquals(Pdu.TYPE_ACK, result.type);
                }
            }
        } finally {
            executor.shutdown();
        }
    }
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) Pdu(herddb.proto.Pdu) Random(java.util.Random) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Aggregations

Pdu (herddb.proto.Pdu)27 Channel (herddb.network.Channel)19 ByteBuf (io.netty.buffer.ByteBuf)17 TimeoutException (java.util.concurrent.TimeoutException)16 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)11 RawString (herddb.utils.RawString)7 Test (org.junit.Test)7 PduCodec (herddb.proto.PduCodec)6 ExecutorService (java.util.concurrent.ExecutorService)6 ChannelEventListener (herddb.network.ChannelEventListener)5 ServerSideConnection (herddb.network.ServerSideConnection)5 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 DumpedLogEntry (herddb.backup.DumpedLogEntry)3 ServerConfiguration (herddb.server.ServerConfiguration)3 DataStorageManagerException (herddb.storage.DataStorageManagerException)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 IOException (java.io.IOException)3