Search in sources :

Example 1 with Pdu

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

the class RoutedClientSideConnection method prepareQuery.

long prepareQuery(String tableSpace, String query) throws HDBException, ClientSideMetadataProviderException {
    long existing = preparedStatements.getQueryId(tableSpace, query);
    if (existing != 0) {
        return existing;
    }
    Channel channel = ensureOpen();
    try {
        long requestId = channel.generateRequestId();
        ByteBuf message = PduCodec.PrepareStatement.write(requestId, tableSpace, query);
        try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
            if (reply.type == Pdu.TYPE_ERROR) {
                handleGenericError(reply, 0);
            } else if (reply.type != Pdu.TYPE_PREPARE_STATEMENT_RESULT) {
                throw new HDBException(reply);
            }
            long statementId = PduCodec.PrepareStatementResult.readStatementId(reply);
            preparedStatements.registerQueryId(tableSpace, query, statementId);
            return statementId;
        }
    } 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 2 with Pdu

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

the class RoutedClientSideConnection method rollbackTransaction.

@Override
public void rollbackTransaction(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_ROLLBACK_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 3 with Pdu

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

the class RoutedClientSideConnection method performAuthentication.

private void performAuthentication(Channel channel, String serverHostname) throws Exception {
    // no need to perform auth in "local" mode
    String mode = connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_STANDALONE);
    if (ClientConfiguration.PROPERTY_MODE_LOCAL.equals(mode) && channel.isLocalChannel()) {
        return;
    }
    SaslNettyClient saslNettyClient = new SaslNettyClient(connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_USERNAME, ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), connection.getClient().getConfiguration().getString(ClientConfiguration.PROPERTY_CLIENT_PASSWORD, ClientConfiguration.PROPERTY_CLIENT_PASSWORD_DEFAULT), serverHostname);
    byte[] firstToken = new byte[0];
    if (saslNettyClient.hasInitialResponse()) {
        firstToken = saslNettyClient.evaluateChallenge(new byte[0]);
    }
    long requestId = channel.generateRequestId();
    Pdu saslResponse = channel.sendMessageWithPduReply(requestId, PduCodec.SaslTokenMessageRequest.write(requestId, SaslUtils.AUTH_DIGEST_MD5, firstToken), timeout);
    try {
        for (int i = 0; i < 100; i++) {
            byte[] responseToSendToServer;
            switch(saslResponse.type) {
                case Pdu.TYPE_SASL_TOKEN_SERVER_RESPONSE:
                    byte[] token = PduCodec.SaslTokenServerResponse.readToken(saslResponse);
                    responseToSendToServer = saslNettyClient.evaluateChallenge(token);
                    requestId = channel.generateRequestId();
                    saslResponse.close();
                    saslResponse = channel.sendMessageWithPduReply(requestId, PduCodec.SaslTokenMessageToken.write(requestId, responseToSendToServer), timeout);
                    if (saslNettyClient.isComplete()) {
                        LOGGER.finest("SASL auth completed with success");
                        return;
                    }
                    break;
                case Pdu.TYPE_ERROR:
                    throw new Exception("Server returned ERROR during SASL negotiation, Maybe authentication failure (" + PduCodec.ErrorResponse.readError(saslResponse) + ")");
                default:
                    throw new Exception("Unexpected server response during SASL negotiation (" + saslResponse + ")");
            }
        }
    } finally {
        saslResponse.close();
    }
    throw new Exception("SASL negotiation took too many steps");
}
Also used : Pdu(herddb.proto.Pdu) SaslNettyClient(herddb.security.sasl.SaslNettyClient) RawString(herddb.utils.RawString) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) LeaderChangedException(herddb.client.impl.LeaderChangedException) RetryRequestException(herddb.client.impl.RetryRequestException) TimeoutException(java.util.concurrent.TimeoutException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) UnreachableServerException(herddb.client.impl.UnreachableServerException) DataStorageManagerException(herddb.storage.DataStorageManagerException)

Example 4 with Pdu

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

the class RoutedClientSideConnection method executeUpdates.

public List<DMLResult> executeUpdates(String tableSpace, String query, long tx, boolean returnValues, boolean usePreparedStatement, List<List<Object>> batch) throws HDBException, ClientSideMetadataProviderException {
    try {
        Channel channel = ensureOpen();
        long requestId = channel.generateRequestId();
        long statementId = usePreparedStatement ? prepareQuery(tableSpace, query) : 0;
        query = statementId > 0 ? "" : query;
        ByteBuf message = PduCodec.ExecuteStatements.write(requestId, tableSpace, query, tx, returnValues, statementId, batch);
        try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
            if (reply.type == Pdu.TYPE_ERROR) {
                handleGenericError(reply, statementId);
                // not possible, handleGenericError always throws an error
                return Collections.emptyList();
            } else if (reply.type != Pdu.TYPE_EXECUTE_STATEMENTS_RESULT) {
                throw new HDBException(reply);
            }
            long transactionId = PduCodec.ExecuteStatementsResult.readTx(reply);
            List<Long> updateCounts = PduCodec.ExecuteStatementsResult.readUpdateCounts(reply);
            int numResults = updateCounts.size();
            List<DMLResult> results = new ArrayList<>(numResults);
            PduCodec.ListOfListsReader resultRecords = PduCodec.ExecuteStatementsResult.startResultRecords(reply);
            int numResultRecords = resultRecords.getNumLists();
            for (int i = 0; i < numResults; i++) {
                Map<RawString, Object> newvalue = null;
                Object key = null;
                if (numResultRecords > 0) {
                    PduCodec.ObjectListReader list = resultRecords.nextList();
                    newvalue = readParametersListAsMap(list);
                    if (newvalue != null) {
                        key = newvalue.get(RAWSTRING_KEY);
                    }
                }
                long updateCount = updateCounts.get(i);
                DMLResult res = new DMLResult(updateCount, key, newvalue, transactionId);
                results.add(res);
            }
            return results;
        }
    } 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) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) PduCodec(herddb.proto.PduCodec) AtomicLong(java.util.concurrent.atomic.AtomicLong) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with Pdu

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

the class RoutedClientSideConnection method beginTransaction.

@Override
public long beginTransaction(String tableSpace) throws HDBException, ClientSideMetadataProviderException {
    Channel channel = ensureOpen();
    try {
        long requestId = channel.generateRequestId();
        ByteBuf message = PduCodec.TxCommand.write(requestId, PduCodec.TxCommand.TX_COMMAND_BEGIN_TRANSACTION, 0, tableSpace);
        try (Pdu reply = channel.sendMessageWithPduReply(requestId, message, timeout)) {
            if (reply.type == Pdu.TYPE_ERROR) {
                handleGenericError(reply, 0);
                // not possible
                return -1;
            } else if (reply.type == Pdu.TYPE_TX_COMMAND_RESULT) {
                long tx = PduCodec.TxCommandResult.readTx(reply);
                if (tx <= 0) {
                    throw new HDBException("Server did not create a new transaction");
                }
                return tx;
            } else {
                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)

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