Search in sources :

Example 21 with Pdu

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);
            }
        }
    }
}
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 22 with Pdu

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");
        }
    }
}
Also used : Pdu(herddb.proto.Pdu) KeyValue(herddb.utils.KeyValue) DumpedLogEntry(herddb.backup.DumpedLogEntry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 23 with Pdu

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;
    }
}
Also used : Pdu(herddb.proto.Pdu) CompletableFuture(java.util.concurrent.CompletableFuture) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 24 with Pdu

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

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);
    }
}
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)

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