Search in sources :

Example 1 with NotLeaderException

use of herddb.model.NotLeaderException 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 NotLeaderException

use of herddb.model.NotLeaderException 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 NotLeaderException

use of herddb.model.NotLeaderException 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 NotLeaderException

use of herddb.model.NotLeaderException in project herddb by diennea.

the class ServerSideConnectionPeer method handleExecuteStatement.

private void handleExecuteStatement(Message message, Channel _channel) {
    Long tx = (Long) message.parameters.get("tx");
    long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
    String query = (String) message.parameters.get("query");
    String tableSpace = (String) message.parameters.get("tableSpace");
    Boolean returnValues = (Boolean) message.parameters.get("returnValues");
    if (returnValues == null) {
        returnValues = Boolean.FALSE;
    }
    List<Object> parameters = (List<Object>) message.parameters.get("params");
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "query " + query + " with " + parameters);
    }
    try {
        TransactionContext transactionContext = new TransactionContext(txId);
        TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
        Statement statement = translatedQuery.plan.mainStatement;
        // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", plan: " + translatedQuery.plan);
        StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
        // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", result:" + result);
        if (result instanceof DMLStatementExecutionResult) {
            DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
            Map<String, Object> otherData = null;
            if (returnValues && dml.getKey() != null) {
                TableAwareStatement tableStatement = statement.unwrap(TableAwareStatement.class);
                Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
                otherData = new HashMap<>();
                Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
                otherData.put("key", key);
                if (dml.getNewvalue() != null) {
                    Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
                    otherData.put("newvalue", newvalue);
                }
            }
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(dml.getUpdateCount(), otherData, dml.transactionId));
        } else if (result instanceof GetResult) {
            GetResult get = (GetResult) result;
            if (!get.found()) {
                _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(0, null, get.transactionId));
            } else {
                Map<String, Object> record = get.getRecord().toBean(get.getTable());
                _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, record, get.transactionId));
            }
        } else if (result instanceof TransactionResult) {
            TransactionResult txresult = (TransactionResult) result;
            Map<String, Object> data = new HashMap<>();
            Set<Long> transactionsForTableSpace = openTransactions.computeIfAbsent(statement.getTableSpace(), k -> new ConcurrentSkipListSet<>());
            switch(txresult.getOutcome()) {
                case BEGIN:
                    {
                        transactionsForTableSpace.add(txresult.getTransactionId());
                        break;
                    }
                case COMMIT:
                case ROLLBACK:
                    transactionsForTableSpace.remove(txresult.getTransactionId());
                    break;
            }
            data.put("tx", txresult.getTransactionId());
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, data, txresult.transactionId));
        } else if (result instanceof DDLStatementExecutionResult) {
            DDLStatementExecutionResult ddl = (DDLStatementExecutionResult) result;
            _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, null, ddl.transactionId));
        } else {
            _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unknown result type " + result.getClass() + " (" + result + ")")));
        }
    } catch (DuplicatePrimaryKeyException err) {
        LOGGER.log(Level.SEVERE, "error on query " + query + ", parameters: " + parameters + ": err", err);
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    } catch (NotLeaderException err) {
        Message error = Message.ERROR(null, err);
        error.setParameter("notLeader", "true");
        _channel.sendReplyMessage(message, error);
    } catch (StatementExecutionException err) {
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    } catch (RuntimeException err) {
        LOGGER.log(Level.SEVERE, "unexpected error on query " + query + ", parameters: " + parameters + ": err", err);
        Message error = Message.ERROR(null, err);
        _channel.sendReplyMessage(message, error);
    }
}
Also used : NotLeaderException(herddb.model.NotLeaderException) Message(herddb.network.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StatementExecutionException(herddb.model.StatementExecutionException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) Record(herddb.model.Record) TransactionResult(herddb.model.TransactionResult) TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) GetResult(herddb.model.GetResult) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) TableAwareStatement(herddb.model.TableAwareStatement) ScanStatement(herddb.model.commands.ScanStatement) Statement(herddb.model.Statement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) TableAwareStatement(herddb.model.TableAwareStatement) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with NotLeaderException

use of herddb.model.NotLeaderException in project herddb by diennea.

the class ServerSideConnectionPeer method handleExecuteStatements.

private void handleExecuteStatements(Message message, Channel _channel) {
    Long tx = (Long) message.parameters.get("tx");
    long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
    long transactionId = txId;
    String query = (String) message.parameters.get("query");
    String tableSpace = (String) message.parameters.get("tableSpace");
    Boolean returnValues = (Boolean) message.parameters.get("returnValues");
    if (returnValues == null) {
        returnValues = Boolean.FALSE;
    }
    List<List<Object>> batch = (List<List<Object>>) message.parameters.get("params");
    try {
        List<Long> updateCounts = new ArrayList<>(batch.size());
        List<Map<String, Object>> otherDatas = new ArrayList<>(batch.size());
        for (int i = 0; i < batch.size(); i++) {
            List<Object> parameters = batch.get(i);
            TransactionContext transactionContext = new TransactionContext(transactionId);
            TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
            Statement statement = translatedQuery.plan.mainStatement;
            StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
            if (transactionId > 0 && result.transactionId > 0 && transactionId != result.transactionId) {
                throw new StatementExecutionException("transactionid changed during batch execution, " + transactionId + "<>" + result.transactionId);
            }
            transactionId = result.transactionId;
            if (result instanceof DMLStatementExecutionResult) {
                DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
                Map<String, Object> otherData = Collections.emptyMap();
                if (returnValues && dml.getKey() != null) {
                    TableAwareStatement tableStatement = (TableAwareStatement) statement;
                    Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
                    Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
                    otherData = new HashMap<>();
                    otherData.put("key", key);
                    if (dml.getNewvalue() != null) {
                        Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
                        otherData.put("newvalue", newvalue);
                    }
                }
                updateCounts.add(Long.valueOf(dml.getUpdateCount()));
                otherDatas.add(otherData);
            } else {
                _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("bad result type " + result.getClass() + " (" + result + ")")));
            }
        }
        _channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULTS(updateCounts, otherDatas, transactionId));
    } catch (HerdDBInternalException 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) HerdDBInternalException(herddb.core.HerdDBInternalException) Message(herddb.network.Message) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) List(java.util.List) TuplesList(herddb.utils.TuplesList) ArrayList(java.util.ArrayList) Record(herddb.model.Record) TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) TableAwareStatement(herddb.model.TableAwareStatement) ScanStatement(herddb.model.commands.ScanStatement) Statement(herddb.model.Statement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) TableAwareStatement(herddb.model.TableAwareStatement) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

NotLeaderException (herddb.model.NotLeaderException)6 StatementExecutionException (herddb.model.StatementExecutionException)6 Message (herddb.network.Message)6 TuplesList (herddb.utils.TuplesList)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 HerdDBInternalException (herddb.core.HerdDBInternalException)4 DataScannerException (herddb.model.DataScannerException)4 DuplicatePrimaryKeyException (herddb.model.DuplicatePrimaryKeyException)4 Record (herddb.model.Record)3 Table (herddb.model.Table)3 TransactionContext (herddb.model.TransactionContext)3 SQLPlannedOperationStatement (herddb.model.commands.SQLPlannedOperationStatement)3 ScanStatement (herddb.model.commands.ScanStatement)3 TranslatedQuery (herddb.sql.TranslatedQuery)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)2 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)2 Statement (herddb.model.Statement)2 StatementExecutionResult (herddb.model.StatementExecutionResult)2