Search in sources :

Example 21 with Channel

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()));
}
Also used : Pdu(herddb.proto.Pdu) InetSocketAddress(java.net.InetSocketAddress) Channel(herddb.network.Channel) ServerSideConnection(herddb.network.ServerSideConnection) ByteBuf(io.netty.buffer.ByteBuf) ChannelEventListener(herddb.network.ChannelEventListener) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) TimeoutException(java.util.concurrent.TimeoutException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 22 with Channel

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Pdu(herddb.proto.Pdu) ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) Server(herddb.server.Server) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) ServerConfiguration(herddb.server.ServerConfiguration) Channel(herddb.network.Channel) NettyChannel(herddb.network.netty.NettyChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) IOException(java.io.IOException) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Test(org.junit.Test)

Example 23 with Channel

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;
}
Also used : Pdu(herddb.proto.Pdu) RawString(herddb.utils.RawString) Channel(herddb.network.Channel) ByteBuf(io.netty.buffer.ByteBuf) PduCodec(herddb.proto.PduCodec) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 24 with Channel

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);
    }
}
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 25 with Channel

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);
    }
}
Also used : Pdu(herddb.proto.Pdu) Channel(herddb.network.Channel) RawString(herddb.utils.RawString) ByteBuf(io.netty.buffer.ByteBuf) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Channel (herddb.network.Channel)27 Pdu (herddb.proto.Pdu)19 TimeoutException (java.util.concurrent.TimeoutException)18 ByteBuf (io.netty.buffer.ByteBuf)17 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)12 Test (org.junit.Test)9 ChannelEventListener (herddb.network.ChannelEventListener)8 ExecutorService (java.util.concurrent.ExecutorService)8 ServerSideConnection (herddb.network.ServerSideConnection)7 RawString (herddb.utils.RawString)7 Random (java.util.Random)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 PduCodec (herddb.proto.PduCodec)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 Message (herddb.network.Message)4 ServerConfiguration (herddb.server.ServerConfiguration)4 DataStorageManagerException (herddb.storage.DataStorageManagerException)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 CompletableFuture (java.util.concurrent.CompletableFuture)4