Search in sources :

Example 1 with DataConsistencyStatementResult

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

the class DataConsistencyCheckTest method tableSpaceConsistencyCheck.

@Test
public void tableSpaceConsistencyCheck() throws Exception {
    String tableSpaceName = "tblspace1";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        execute(manager, "CREATE TABLESPACE 'tblspace1'", Collections.emptyList());
        manager.waitForTablespace("tblspace1", 10000);
        manager.waitForTablespace(tableSpaceName, 10000, false);
        Table table = Table.builder().tablespace(tableSpaceName).name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        Table table2 = Table.builder().tablespace(tableSpaceName).name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.BOOLEAN).primaryKey("id").build();
        CreateTableStatement st = new CreateTableStatement(table);
        manager.executeStatement(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        CreateTableStatement st2 = new CreateTableStatement(table2);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        execute(manager, "INSERT INTO tblspace1.t1 (id,name) values (?,?)", Arrays.asList("1", true));
        execute(manager, "INSERT INTO tblspace1.t1 (id,name) values (?,?)", Arrays.asList("2", false));
        execute(manager, "INSERT INTO tblspace1.t2 (id,name) values (?,?)", Arrays.asList("1", true));
        execute(manager, "INSERT INTO tblspace1.t2 (id,name) values (?,?)", Arrays.asList("2", false));
        TableSpaceConsistencyCheckStatement statement = new TableSpaceConsistencyCheckStatement(tableSpaceName);
        DataConsistencyStatementResult result = manager.createTableSpaceCheckSum(statement);
        assertTrue("Check tableSpace  ", result.getOk());
    }
}
Also used : TableSpaceConsistencyCheckStatement(herddb.model.commands.TableSpaceConsistencyCheckStatement) DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 2 with DataConsistencyStatementResult

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

the class DataConsistencyCheckTest method emptyTableConsistencyCheck.

@Test
public void emptyTableConsistencyCheck() throws Exception {
    String tableSpaceName = "tblspace1";
    String tableName = "t1";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        execute(manager, "CREATE TABLESPACE 'tblspace1'", Collections.emptyList());
        manager.waitForTablespace("tblspace1", 10000);
        Table table = Table.builder().tablespace(tableSpaceName).name(tableName).column("k1", ColumnTypes.STRING).column("n1", ColumnTypes.INTEGER).column("s1", ColumnTypes.STRING).primaryKey("k1").build();
        CreateTableStatement st = new CreateTableStatement(table);
        manager.executeStatement(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        // check empty table
        TableConsistencyCheckStatement statement = new TableConsistencyCheckStatement(tableName, tableSpaceName);
        DataConsistencyStatementResult result = manager.createTableCheckSum(statement, null);
        assertTrue("Check empty table ", result.getOk());
    }
}
Also used : DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement) Test(org.junit.Test)

Example 3 with DataConsistencyStatementResult

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

the class DataConsistencyCheckTest method tableWithNullValueConsistencyCheck.

@Test
public void tableWithNullValueConsistencyCheck() throws Exception {
    String tableSpaceName = "t1";
    String tableName = "nulltable";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        execute(manager, "CREATE TABLESPACE 't1'", Collections.emptyList());
        manager.waitForTablespace(tableSpaceName, 10000);
        Table table = Table.builder().tablespace(tableSpaceName).name(tableName).column("k1", ColumnTypes.INTEGER).column("n1", ColumnTypes.INTEGER).column("s1", ColumnTypes.STRING).primaryKey("k1", true).build();
        CreateTableStatement st = new CreateTableStatement(table);
        manager.executeStatement(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        execute(manager, "INSERT INTO " + tableSpaceName + "." + tableName + " (n1 ,s1) values (?,?)", Arrays.asList(null, null));
        execute(manager, "INSERT INTO " + tableSpaceName + "." + tableName + " (n1 ,s1) values (?,?)", Arrays.asList(null, null));
        execute(manager, "INSERT INTO " + tableSpaceName + "." + tableName + " (n1 ,s1) values (?,?)", Arrays.asList(null, null));
        execute(manager, "INSERT INTO " + tableSpaceName + "." + tableName + " (n1 ,s1) values (?,?)", Arrays.asList(null, null));
        TableConsistencyCheckStatement statement = new TableConsistencyCheckStatement(tableName, tableSpaceName);
        DataConsistencyStatementResult result = manager.createTableCheckSum(statement, null);
        assertTrue("Check table with null value ", result.getOk());
    }
}
Also used : DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement) Test(org.junit.Test)

Example 4 with DataConsistencyStatementResult

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

the class ServerSideConnectionPeer method handleExecuteStatement.

private void handleExecuteStatement(Pdu message, Channel channel) {
    long txId = PduCodec.ExecuteStatement.readTx(message);
    String tablespace = PduCodec.ExecuteStatement.readTablespace(message);
    long statementId = PduCodec.ExecuteStatement.readStatementId(message);
    String query = statementId > 0 ? preparedStatements.resolveQuery(tablespace, statementId) : PduCodec.ExecuteStatement.readQuery(message);
    if (query == null) {
        ByteBuf error = PduCodec.ErrorResponse.writeMissingPreparedStatementError(message.messageId, "bad statement id: " + statementId);
        channel.sendReplyMessage(message.messageId, error);
        message.close();
        return;
    }
    boolean returnValues = PduCodec.ExecuteStatement.readReturnValues(message);
    PduCodec.ObjectListReader parametersReader = PduCodec.ExecuteStatement.startReadParameters(message);
    List<Object> parameters = new ArrayList<>(parametersReader.getNumParams());
    for (int i = 0; i < parametersReader.getNumParams(); i++) {
        parameters.add(parametersReader.nextObject());
    }
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "query {0} with {1}", new Object[] { query, parameters });
    }
    RunningStatementInfo statementInfo = new RunningStatementInfo(query, System.currentTimeMillis(), tablespace, "", 1);
    TransactionContext transactionContext = new TransactionContext(txId);
    TranslatedQuery translatedQuery;
    try {
        translatedQuery = server.getManager().getPlanner().translate(tablespace, query, parameters, false, true, returnValues, -1);
    } catch (StatementExecutionException ex) {
        ByteBuf error = composeErrorResponse(message.messageId, ex);
        channel.sendReplyMessage(message.messageId, error);
        message.close();
        return;
    }
    Statement statement = translatedQuery.plan.mainStatement;
    // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", plan: " + translatedQuery.plan);
    RunningStatementsStats runningStatements = server.getManager().getRunningStatements();
    runningStatements.registerRunningStatement(statementInfo);
    CompletableFuture<StatementExecutionResult> res = server.getManager().executePlanAsync(translatedQuery.plan, translatedQuery.context, transactionContext);
    // LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", result:" + result);
    res.whenComplete((result, err) -> {
        try {
            runningStatements.unregisterRunningStatement(statementInfo);
            if (err != null) {
                while (err instanceof CompletionException) {
                    err = err.getCause();
                }
                if (err instanceof DuplicatePrimaryKeyException) {
                    ByteBuf error = PduCodec.ErrorResponse.writeSqlIntegrityConstraintsViolation(message.messageId, new SQLIntegrityConstraintViolationException(err));
                    channel.sendReplyMessage(message.messageId, error);
                } else if (err instanceof NotLeaderException) {
                    ByteBuf error = composeErrorResponse(message.messageId, err);
                    channel.sendReplyMessage(message.messageId, error);
                } else if (err instanceof StatementExecutionException) {
                    ByteBuf error = composeErrorResponse(message.messageId, err);
                    channel.sendReplyMessage(message.messageId, error);
                } else {
                    LOGGER.log(Level.SEVERE, "unexpected error on query " + query + ", parameters: " + parameters + ":" + err, err);
                    ByteBuf error = composeErrorResponse(message.messageId, err);
                    channel.sendReplyMessage(message.messageId, error);
                }
                return;
            }
            if (result instanceof DMLStatementExecutionResult) {
                DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
                Map<String, Object> newRecord = null;
                if (returnValues && dml.getKey() != null) {
                    TableAwareStatement tableStatement = statement.unwrap(TableAwareStatement.class);
                    Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
                    newRecord = new HashMap<>();
                    Object newKey = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
                    newRecord.put("_key", newKey);
                    if (dml.getNewvalue() != null) {
                        newRecord.putAll(RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table));
                    }
                }
                channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, dml.getUpdateCount(), dml.transactionId, newRecord));
            } else if (result instanceof GetResult) {
                GetResult get = (GetResult) result;
                if (!get.found()) {
                    channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, 0, get.transactionId, null));
                } else {
                    Map<String, Object> record = get.getRecord().toBean(get.getTable());
                    channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, 1, get.transactionId, record));
                }
            } else if (result instanceof TransactionResult) {
                TransactionResult txresult = (TransactionResult) result;
                channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, 1, txresult.getTransactionId(), null));
            } else if (result instanceof DDLStatementExecutionResult) {
                DDLStatementExecutionResult ddl = (DDLStatementExecutionResult) result;
                channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, 1, ddl.transactionId, null));
            } else if (result instanceof DataConsistencyStatementResult) {
                channel.sendReplyMessage(message.messageId, PduCodec.ExecuteStatementResult.write(message.messageId, 0, 0, null));
            } else {
                ByteBuf error = PduCodec.ErrorResponse.write(message.messageId, "unknown result type:" + result);
                channel.sendReplyMessage(message.messageId, error);
            }
        } finally {
            message.close();
        }
    });
}
Also used : NotLeaderException(herddb.model.NotLeaderException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) RawString(herddb.utils.RawString) ByteBuf(io.netty.buffer.ByteBuf) RunningStatementInfo(herddb.core.RunningStatementInfo) StatementExecutionException(herddb.model.StatementExecutionException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Record(herddb.model.Record) RunningStatementsStats(herddb.core.RunningStatementsStats) TransactionResult(herddb.model.TransactionResult) TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) TableAwareStatement(herddb.model.TableAwareStatement) ScanStatement(herddb.model.commands.ScanStatement) Statement(herddb.model.Statement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) PduCodec(herddb.proto.PduCodec) TableAwareStatement(herddb.model.TableAwareStatement) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) CompletionException(java.util.concurrent.CompletionException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with DataConsistencyStatementResult

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

the class DBManager method createTableCheckSum.

public DataConsistencyStatementResult createTableCheckSum(TableConsistencyCheckStatement tableConsistencyCheckStatement, StatementEvaluationContext context) {
    TableSpaceManager manager = tablesSpaces.get(tableConsistencyCheckStatement.getTableSpace());
    String tableName = tableConsistencyCheckStatement.getTable();
    String tableSpaceName = tableConsistencyCheckStatement.getTableSpace();
    if (manager == null) {
        return new DataConsistencyStatementResult(false, "No such tablespace " + tableSpaceName);
    }
    try {
        manager.createAndWriteTableCheksum(manager, tableSpaceName, tableName, context);
    } catch (IOException | DataScannerException ex) {
        LOGGER.log(Level.SEVERE, "Error on check of tablespace " + tableSpaceName, ex);
        return new DataConsistencyStatementResult(false, "Error on check of tablespace " + tableSpaceName + ":" + ex);
    }
    return new DataConsistencyStatementResult(true, "Check table consistency for " + tableName + "completed");
}
Also used : DataConsistencyStatementResult(herddb.model.DataConsistencyStatementResult) IOException(java.io.IOException) DataScannerException(herddb.model.DataScannerException)

Aggregations

DataConsistencyStatementResult (herddb.model.DataConsistencyStatementResult)9 Table (herddb.model.Table)8 DBManager (herddb.core.DBManager)6 CreateTableStatement (herddb.model.commands.CreateTableStatement)6 Test (org.junit.Test)6 TableConsistencyCheckStatement (herddb.model.commands.TableConsistencyCheckStatement)5 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)4 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)4 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)4 DataScannerException (herddb.model.DataScannerException)2 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)2 SystemInstrumentation (herddb.utils.SystemInstrumentation)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RunningStatementInfo (herddb.core.RunningStatementInfo)1 RunningStatementsStats (herddb.core.RunningStatementsStats)1 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)1 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)1 DuplicatePrimaryKeyException (herddb.model.DuplicatePrimaryKeyException)1