Search in sources :

Example 1 with Message

use of herddb.network.Message in project herddb by diennea.

the class ServerSideConnectionPeer method handlePushTxLogChunk.

private void handlePushTxLogChunk(Message message, Channel _channel) {
    try {
        String tableSpace = (String) message.parameters.get("tableSpace");
        List<KeyValue> data = (List<KeyValue>) message.parameters.get("data");
        LOGGER.log(Level.INFO, "Received " + data.size() + " records for restore of txlog in tableSpace " + tableSpace);
        List<DumpedLogEntry> entries = new ArrayList<>(data.size());
        for (KeyValue kv : data) {
            entries.add(new DumpedLogEntry(LogSequenceNumber.deserialize(kv.key), kv.value));
        }
        server.getManager().getTableSpaceManager(tableSpace).restoreRawDumpedEntryLogs(entries);
        _channel.sendReplyMessage(message, Message.ACK(null));
    } catch (Exception err) {
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) KeyValue(herddb.network.KeyValue) DumpedLogEntry(herddb.backup.DumpedLogEntry) Message(herddb.network.Message) ArrayList(java.util.ArrayList) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException)

Example 2 with Message

use of herddb.network.Message in project herddb by diennea.

the class ServerSideConnectionPeer method handleOpenScanner.

private void handleOpenScanner(Message message, Channel _channel) {
    String tableSpace = (String) message.parameters.get("tableSpace");
    Long tx = (Long) message.parameters.get("tx");
    long txId = tx != null ? tx : 0;
    String query = (String) message.parameters.get("query");
    String scannerId = (String) message.parameters.get("scannerId");
    int fetchSize = 10;
    if (message.parameters.containsKey("fetchSize")) {
        fetchSize = (Integer) message.parameters.get("fetchSize");
    }
    int maxRows = 0;
    if (message.parameters.containsKey("maxRows")) {
        maxRows = (Integer) message.parameters.get("maxRows");
    }
    List<Object> parameters = (List<Object>) message.parameters.get("params");
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "openScanner txId+" + txId + ", fetchSize " + fetchSize + ", maxRows " + maxRows + "," + query + " with " + parameters);
    }
    try {
        TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, true, true, false, maxRows);
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.log(Level.FINEST, query + " -> " + translatedQuery.plan.mainStatement);
        }
        TransactionContext transactionContext = new TransactionContext(txId);
        if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement || translatedQuery.plan.joinStatements != null) {
            ScanResult scanResult = (ScanResult) server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
            DataScanner dataScanner = scanResult.dataScanner;
            ServerSideScannerPeer scanner = new ServerSideScannerPeer(dataScanner);
            String[] columns = dataScanner.getFieldNames();
            List<DataAccessor> records = dataScanner.consume(fetchSize);
            TuplesList tuplesList = new TuplesList(columns, records);
            boolean last = dataScanner.isFinished();
            LOGGER.log(Level.FINEST, "sending first {0} records to scanner {1} query {2}", new Object[] { records.size(), scannerId, query });
            if (!last) {
                scanners.put(scannerId, scanner);
            }
            _channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
        } else {
            _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unsupported query type for scan " + query + ": PLAN is " + translatedQuery.plan)));
        }
    } catch (DataScannerException | RuntimeException err) {
        LOGGER.log(Level.SEVERE, "error on scanner " + scannerId + ": " + err, err);
        scanners.remove(scannerId);
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Message(herddb.network.Message) DataAccessor(herddb.utils.DataAccessor) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DataScanner(herddb.model.DataScanner) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) ScanStatement(herddb.model.commands.ScanStatement) TuplesList(herddb.utils.TuplesList) DataScannerException(herddb.model.DataScannerException) ScanResult(herddb.model.ScanResult) TranslatedQuery(herddb.sql.TranslatedQuery) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 3 with Message

use of herddb.network.Message in project herddb by diennea.

the class ServerSideConnectionPeer method handleRequestTableRestore.

private void handleRequestTableRestore(Message message, Channel _channel) {
    try {
        String tableSpace = (String) message.parameters.get("tableSpace");
        byte[] table = (byte[]) message.parameters.get("table");
        long dumpLedgerId = (long) message.parameters.get("dumpLedgerId");
        long dumpOffset = (long) message.parameters.get("dumpOffset");
        Table tableSchema = Table.deserialize(table);
        tableSchema = Table.builder().cloning(tableSchema).tablespace(tableSpace).build();
        server.getManager().getTableSpaceManager(tableSpace).beginRestoreTable(tableSchema.serialize(), new LogSequenceNumber(dumpLedgerId, dumpOffset));
        _channel.sendReplyMessage(message, Message.ACK(null));
    } catch (StatementExecutionException err) {
        Message error = Message.ERROR(null, err);
        if (err instanceof NotLeaderException) {
            error.setParameter("notLeader", "true");
        }
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Table(herddb.model.Table) Message(herddb.network.Message) LogSequenceNumber(herddb.log.LogSequenceNumber) StatementExecutionException(herddb.model.StatementExecutionException)

Example 4 with Message

use of herddb.network.Message in project herddb by diennea.

the class MessageUtils method decodeMessage.

public static Message decodeMessage(ByteBuf encoded) {
    byte version = encoded.readByte();
    if (version != VERSION) {
        throw new RuntimeException("bad protocol version " + version);
    }
    int type = ByteBufUtils.readVInt(encoded);
    String messageId = readUTF8String(encoded);
    String replyMessageId = null;
    String workerProcessId = null;
    Map<String, Object> params = new HashMap<>();
    while (encoded.isReadable()) {
        byte opcode = encoded.readByte();
        switch(opcode) {
            case OPCODE_REPLYMESSAGEID:
                replyMessageId = readUTF8String(encoded);
                break;
            case OPCODE_WORKERPROCESSID:
                workerProcessId = readUTF8String(encoded);
                break;
            case OPCODE_PARAMETERS:
                int size = ByteBufUtils.readVInt(encoded);
                for (int i = 0; i < size; i++) {
                    Object key = readEncodedSimpleValue(encoded);
                    Object value = readEncodedSimpleValue(encoded);
                    params.put((String) key, value);
                }
                break;
            default:
                throw new RuntimeException("invalid opcode: " + opcode);
        }
    }
    Message m = new Message(workerProcessId, type, params);
    if (replyMessageId != null) {
        m.replyMessageId = replyMessageId;
    }
    m.messageId = messageId;
    return m;
}
Also used : Message(herddb.network.Message) HashMap(java.util.HashMap) RawString(herddb.utils.RawString)

Example 5 with Message

use of herddb.network.Message in project herddb by diennea.

the class NettyChannel method handleReply.

private void handleReply(Message anwermessage) {
    final ReplyCallback callback = pendingReplyMessages.get(anwermessage.getReplyMessageId());
    pendingReplyMessages.remove(anwermessage.getReplyMessageId());
    pendingReplyMessagesDeadline.remove(anwermessage.getReplyMessageId());
    Message original = pendingReplyMessagesSource.remove(anwermessage.getReplyMessageId());
    if (callback != null && original != null) {
        submitCallback(() -> {
            callback.replyReceived(original, anwermessage, null);
        });
    }
}
Also used : Message(herddb.network.Message) ReplyCallback(herddb.network.ReplyCallback)

Aggregations

Message (herddb.network.Message)22 StatementExecutionException (herddb.model.StatementExecutionException)10 NotLeaderException (herddb.model.NotLeaderException)9 ArrayList (java.util.ArrayList)9 TuplesList (herddb.utils.TuplesList)8 HerdDBInternalException (herddb.core.HerdDBInternalException)7 DataScannerException (herddb.model.DataScannerException)7 DuplicatePrimaryKeyException (herddb.model.DuplicatePrimaryKeyException)7 List (java.util.List)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 HashMap (java.util.HashMap)6 Table (herddb.model.Table)5 Record (herddb.model.Record)4 TransactionContext (herddb.model.TransactionContext)4 SQLPlannedOperationStatement (herddb.model.commands.SQLPlannedOperationStatement)4 ScanStatement (herddb.model.commands.ScanStatement)4 Channel (herddb.network.Channel)4 KeyValue (herddb.network.KeyValue)4 DataAccessor (herddb.utils.DataAccessor)4 Map (java.util.Map)4