Search in sources :

Example 11 with DataScannerException

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

the class DBManager method executeJoinedScansPlan.

private StatementExecutionResult executeJoinedScansPlan(List<DataScanner> scanResults, StatementEvaluationContext context, TransactionContext transactionContext, ExecutionPlan plan) throws StatementExecutionException {
    try {
        List<Column> composedSchema = new ArrayList<>();
        for (DataScanner ds : scanResults) {
            composedSchema.addAll(Arrays.asList(ds.getSchema()));
        }
        Column[] finalSchema = new Column[composedSchema.size()];
        composedSchema.toArray(finalSchema);
        String[] finalSchemaFieldNames = Column.buildFieldNamesList(finalSchema);
        MaterializedRecordSet finalResultSet = recordSetFactory.createRecordSet(finalSchemaFieldNames, finalSchema);
        DataScannerJoinExecutor joinExecutor;
        if (plan.joinProjection != null) {
            if (plan.joinFilter != null) {
                TuplePredicate joinFilter = plan.joinFilter;
                joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
                    if (joinFilter.matches(t, context)) {
                        finalResultSet.add(plan.joinProjection.map(t, context));
                    }
                });
            } else {
                joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
                    finalResultSet.add(plan.joinProjection.map(t, context));
                });
            }
        } else {
            if (plan.joinFilter != null) {
                TuplePredicate joinFilter = plan.joinFilter;
                joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
                    if (joinFilter.matches(t, context)) {
                        finalResultSet.add(t);
                    }
                });
            } else {
                joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
                    finalResultSet.add(t);
                });
            }
        }
        joinExecutor.executeJoin();
        finalResultSet.writeFinished();
        finalResultSet.sort(plan.comparator);
        finalResultSet.applyLimits(plan.limits, context);
        return new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, finalResultSet));
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ExecutionPlan(herddb.model.ExecutionPlan) SQLStatementEvaluationContext(herddb.sql.SQLStatementEvaluationContext) Arrays(java.util.Arrays) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) JMXUtils(herddb.jmx.JMXUtils) ClientConfiguration(herddb.client.ClientConfiguration) MetadataStorageManager(herddb.metadata.MetadataStorageManager) Channel(herddb.network.Channel) ServerConfiguration(herddb.server.ServerConfiguration) Map(java.util.Map) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) ConnectionsInfoProvider(herddb.core.stats.ConnectionsInfoProvider) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) ThreadFactory(java.util.concurrent.ThreadFactory) Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) DropTableSpaceStatement(herddb.model.commands.DropTableSpaceStatement) ScanResult(herddb.model.ScanResult) DataAccessor(herddb.utils.DataAccessor) LogNotAvailableException(herddb.log.LogNotAvailableException) DataScannerJoinExecutor(herddb.core.join.DataScannerJoinExecutor) InsertStatement(herddb.model.commands.InsertStatement) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) Collection(java.util.Collection) AbstractSQLPlanner(herddb.sql.AbstractSQLPlanner) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) Message(herddb.network.Message) NotLeaderException(herddb.model.NotLeaderException) GetStatement(herddb.model.commands.GetStatement) NodeMetadata(herddb.model.NodeMetadata) DefaultJVMHalt(herddb.utils.DefaultJVMHalt) DDLStatement(herddb.model.DDLStatement) TableSpace(herddb.model.TableSpace) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Statement(herddb.model.Statement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) CalcitePlanner(herddb.sql.CalcitePlanner) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) MetadataChangeListener(herddb.metadata.MetadataChangeListener) GetResult(herddb.model.GetResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) SQLPlanner(herddb.sql.SQLPlanner) TransactionContext(herddb.model.TransactionContext) CommitLogManager(herddb.log.CommitLogManager) ManagementFactory(java.lang.management.ManagementFactory) ExecutorService(java.util.concurrent.ExecutorService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatement(herddb.model.DMLStatement) LimitedDataScanner(herddb.model.LimitedDataScanner) DataStorageManager(herddb.storage.DataStorageManager) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) DBManagerStatsMXBean(herddb.jmx.DBManagerStatsMXBean) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) CommitLog(herddb.log.CommitLog) TuplePredicate(herddb.model.TuplePredicate) Column(herddb.model.Column) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Collections(java.util.Collections) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ServerHostData(herddb.network.ServerHostData) TuplePredicate(herddb.model.TuplePredicate) ScanResult(herddb.model.ScanResult) ArrayList(java.util.ArrayList) DataScannerJoinExecutor(herddb.core.join.DataScannerJoinExecutor) StatementExecutionException(herddb.model.StatementExecutionException) DataScanner(herddb.model.DataScanner) LimitedDataScanner(herddb.model.LimitedDataScanner) Column(herddb.model.Column) DataScannerException(herddb.model.DataScannerException)

Example 12 with DataScannerException

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

the class DeleteOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    final Table table = tableSpaceManager.getTableManager(tableName).getTable();
    long transactionId = transactionContext.transactionId;
    int updateCount = 0;
    Bytes key = null;
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
            DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, null, new RawKeyEquals(key));
            DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(deleteStatement, context, transactionContext);
            updateCount += _result.getUpdateCount();
            if (_result.transactionId > 0 && _result.transactionId != transactionId) {
                transactionId = _result.transactionId;
                transactionContext = new TransactionContext(transactionId);
            }
            key = _result.getKey();
        }
        return new DMLStatementExecutionResult(transactionId, updateCount, key, null);
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ScanResult(herddb.model.ScanResult) RawKeyEquals(herddb.model.predicates.RawKeyEquals) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) DeleteStatement(herddb.model.commands.DeleteStatement) StatementExecutionException(herddb.model.StatementExecutionException) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) DMLStatement(herddb.model.DMLStatement) DataScannerException(herddb.model.DataScannerException)

Example 13 with DataScannerException

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

the class UpdateOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    final Table table = tableSpaceManager.getTableManager(tableName).getTable();
    long transactionId = transactionContext.transactionId;
    int updateCount = 0;
    Bytes key = null;
    Bytes newValue = null;
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
            Predicate pred = new RawKeyEquals(key);
            DMLStatement updateStatement = new UpdateStatement(tableSpace, tableName, null, this.recordFunction, pred).setReturnValues(returnValues);
            DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(updateStatement, context, transactionContext);
            updateCount += _result.getUpdateCount();
            if (_result.transactionId > 0 && _result.transactionId != transactionId) {
                transactionId = _result.transactionId;
                transactionContext = new TransactionContext(transactionId);
            }
            key = _result.getKey();
            newValue = _result.getNewvalue();
        }
        return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ScanResult(herddb.model.ScanResult) RawKeyEquals(herddb.model.predicates.RawKeyEquals) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(herddb.model.Predicate) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) DMLStatement(herddb.model.DMLStatement) DataScannerException(herddb.model.DataScannerException)

Example 14 with DataScannerException

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

the class ServerSideConnectionPeer method handleFetchScannerData.

private void handleFetchScannerData(Message message, Channel _channel) {
    String scannerId = (String) message.parameters.get("scannerId");
    int fetchSize = (Integer) message.parameters.get("fetchSize");
    ServerSideScannerPeer scanner = scanners.get(scannerId);
    if (scanner != null) {
        try {
            DataScanner dataScanner = scanner.getScanner();
            List<DataAccessor> records = dataScanner.consume(fetchSize);
            String[] columns = dataScanner.getFieldNames();
            TuplesList tuplesList = new TuplesList(columns, records);
            boolean last = false;
            if (dataScanner.isFinished()) {
                LOGGER.log(Level.FINEST, "unregistering scanner {0}, resultset is finished", scannerId);
                scanners.remove(scannerId);
                last = true;
            }
            // LOGGER.log(Level.SEVERE, "sending " + converted.size() + " records to scanner " + scannerId);
            _channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
        } catch (DataScannerException error) {
            _channel.sendReplyMessage(message, Message.ERROR(null, error).setParameter("scannerId", scannerId));
        }
    } else {
        _channel.sendReplyMessage(message, Message.ERROR(null, new Exception("no such scanner " + scannerId + ", only " + scanners.keySet())).setParameter("scannerId", scannerId));
    }
}
Also used : DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) TuplesList(herddb.utils.TuplesList) DataScannerException(herddb.model.DataScannerException)

Example 15 with DataScannerException

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

the class TableDataChecksum method createChecksum.

public static TableChecksum createChecksum(DBManager manager, TranslatedQuery query, TableSpaceManager tableSpaceManager, String tableSpace, String tableName) throws DataScannerException {
    AbstractTableManager tablemanager = tableSpaceManager.getTableManager(tableName);
    String nodeID = tableSpaceManager.getDbmanager().getNodeId();
    TranslatedQuery translated = query;
    final Table table = manager.getTableSpaceManager(tableSpace).getTableManager(tableName).getTable();
    // Number of records
    long nrecords = 0;
    // For example, in leader node we may not know the query
    if (translated == null) {
        String columns = formatColumns(table);
        /*
                scan = true
                allowCache = false
                returnValues = false
                maxRows = -1
            */
        translated = manager.getPlanner().translate(tableSpace, "SELECT  " + columns + " FROM " + tableName + " order by " + formatPrimaryKeys(table), Collections.emptyList(), true, false, false, -1);
    }
    ScanStatement statement = translated.plan.mainStatement.unwrap(ScanStatement.class);
    statement.setAllowExecutionFromFollower(true);
    LOGGER.log(Level.INFO, "creating checksum for table {0}.{1} on node {2}", new Object[] { tableSpace, tableName, nodeID });
    try (DataScanner scan = manager.scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
        StreamingXXHash64 hash64 = FACTORY.newStreamingHash64(SEED);
        long _start = System.currentTimeMillis();
        while (scan.hasNext()) {
            nrecords++;
            DataAccessor data = scan.next();
            data.forEach((String key, Object value) -> {
                int type = table.getColumn(key).type;
                byte[] serialize = RecordSerializer.serialize(value, type);
                /*
                        Update need three parameters
                        update(byte[]buff, int off, int len)
                        buff is the input data
                        off is the start offset in buff
                        len is the number of bytes to hash
                     */
                if (serialize != null) {
                    hash64.update(serialize, 0, serialize.length);
                }
            });
        }
        LOGGER.log(Level.INFO, "Number of processed records for table {0}.{1} on node {2} = {3} ", new Object[] { tableSpace, tableName, nodeID, nrecords });
        long _stop = System.currentTimeMillis();
        long nextAutoIncrementValue = tablemanager.getNextPrimaryKeyValue();
        long scanduration = (_stop - _start);
        LOGGER.log(Level.INFO, "Creating checksum for table {0}.{1} on node {2} finished in {3} ms", new Object[] { tableSpace, tableName, nodeID, scanduration });
        SystemInstrumentation.instrumentationPoint("createChecksum", tableSpace, tableName);
        return new TableChecksum(tableSpace, tableName, hash64.getValue(), HASH_TYPE, nrecords, nextAutoIncrementValue, translated.context.query, scanduration);
    } catch (DataScannerException ex) {
        LOGGER.log(Level.SEVERE, "Scan failled", ex);
        throw new DataScannerException(ex);
    }
}
Also used : TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) StreamingXXHash64(net.jpountz.xxhash.StreamingXXHash64) DataScanner(herddb.model.DataScanner) AbstractTableManager(herddb.core.AbstractTableManager) ScanStatement(herddb.model.commands.ScanStatement) DataScannerException(herddb.model.DataScannerException)

Aggregations

DataScannerException (herddb.model.DataScannerException)30 DataScanner (herddb.model.DataScanner)26 StatementExecutionException (herddb.model.StatementExecutionException)25 DataAccessor (herddb.utils.DataAccessor)24 ScanResult (herddb.model.ScanResult)19 TransactionContext (herddb.model.TransactionContext)18 StatementExecutionResult (herddb.model.StatementExecutionResult)15 Table (herddb.model.Table)13 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)12 ArrayList (java.util.ArrayList)12 Bytes (herddb.utils.Bytes)10 DMLStatement (herddb.model.DMLStatement)8 InsertStatement (herddb.model.commands.InsertStatement)8 ScanStatement (herddb.model.commands.ScanStatement)8 Column (herddb.model.Column)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 HerdDBInternalException (herddb.core.HerdDBInternalException)5 DuplicatePrimaryKeyException (herddb.model.DuplicatePrimaryKeyException)5 RecordFunction (herddb.model.RecordFunction)5