use of herddb.model.commands.TableConsistencyCheckStatement in project herddb by diennea.
the class TableManager method executeStatementAsync.
@Override
public CompletableFuture<StatementExecutionResult> executeStatementAsync(Statement statement, Transaction transaction, StatementEvaluationContext context) {
CompletableFuture<StatementExecutionResult> res;
long lockStamp = checkpointLock.readLock();
if (statement instanceof UpdateStatement) {
UpdateStatement update = (UpdateStatement) statement;
res = executeUpdateAsync(update, transaction, context);
} else if (statement instanceof InsertStatement) {
InsertStatement insert = (InsertStatement) statement;
res = executeInsertAsync(insert, transaction, context);
} else if (statement instanceof GetStatement) {
GetStatement get = (GetStatement) statement;
res = executeGetAsync(get, transaction, context);
} else if (statement instanceof DeleteStatement) {
DeleteStatement delete = (DeleteStatement) statement;
res = executeDeleteAsync(delete, transaction, context);
} else if (statement instanceof TruncateTableStatement) {
try {
TruncateTableStatement truncate = (TruncateTableStatement) statement;
res = CompletableFuture.completedFuture(executeTruncate(truncate, transaction, context));
} catch (StatementExecutionException err) {
LOGGER.log(Level.SEVERE, "Truncate table failed", err);
res = Futures.exception(err);
}
} else if (statement instanceof TableConsistencyCheckStatement) {
DBManager manager = this.tableSpaceManager.getDbmanager();
res = CompletableFuture.completedFuture(manager.createTableCheckSum((TableConsistencyCheckStatement) statement, context));
} else {
res = Futures.exception(new StatementExecutionException("not implemented " + statement.getClass()));
}
res = res.whenComplete((r, error) -> {
checkpointLock.unlockRead(lockStamp);
});
if (statement instanceof TruncateTableStatement) {
res = res.whenComplete((r, error) -> {
if (error == null) {
try {
flush();
} catch (DataStorageManagerException err) {
throw new HerdDBInternalException(new StatementExecutionException("internal data error: " + err, err));
}
}
});
}
return res;
}
use of herddb.model.commands.TableConsistencyCheckStatement in project herddb by diennea.
the class JSQLParserPlanner method queryConsistencyCheckStatement.
public Statement queryConsistencyCheckStatement(String defaultTablespace, String query, List<Object> parameters) {
if (query.startsWith(TABLE_CONSISTENCY_COMMAND)) {
query = query.substring(query.substring(0, 21).length());
String tableSpace = defaultTablespace;
String tableName;
if (query.contains(".")) {
String[] tokens = query.split("\\.");
tableSpace = tokens[0].trim().replaceAll("\'", "");
tableName = tokens[1].trim().replaceAll("\'", "");
} else {
tableName = query.trim();
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new TableSpaceDoesNotExistException(String.format("Tablespace %s does not exist.", tableSpace));
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null || tableManager.getCreatedInTransaction() > 0) {
throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
}
return new TableConsistencyCheckStatement(tableName, tableSpace);
} else {
throw new StatementExecutionException(String.format("Incorrect Syntax for tableconsistencycheck"));
}
}
use of herddb.model.commands.TableConsistencyCheckStatement 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());
}
}
use of herddb.model.commands.TableConsistencyCheckStatement 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());
}
}
use of herddb.model.commands.TableConsistencyCheckStatement in project herddb by diennea.
the class ConsistencyCheckDuringRecoveryTest method consistencyCheckReplicaTest.
@Test
public void consistencyCheckReplicaTest() throws Exception {
final AtomicInteger callCount = new AtomicInteger();
SystemInstrumentation.addListener(new SystemInstrumentation.SingleInstrumentationPointListener("createChecksum") {
@Override
public void acceptSingle(Object... args) throws Exception {
callCount.incrementAndGet();
}
});
try (DBManager manager1 = startDBManager("node1")) {
manager1.executeStatement(new CreateTableSpaceStatement(tableSpaceName, new HashSet<>(Arrays.asList("node1", "node2")), "node1", 1, 0, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager1.waitForTablespace(tableSpaceName, 10000, true);
try (DBManager manager2 = startDBManager("node2")) {
manager2.waitForTablespace(tableSpaceName, 10000, false);
Table table = Table.builder().tablespace(tableSpaceName).name(tableName).column("id", ColumnTypes.STRING).column("name", ColumnTypes.BOOLEAN).primaryKey("id").build();
CreateTableStatement st = new CreateTableStatement(table);
manager1.executeStatement(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager1.waitForTable(tableSpaceName, tableName, 10000, true);
execute(manager1, "INSERT INTO t2.table1 (id,name) values (?,?)", Arrays.asList("1", true));
execute(manager1, "INSERT INTO t2.table1 (id,name) values (?,?)", Arrays.asList("2", false));
manager2.waitForTable(tableSpaceName, tableName, 10000, false);
TableConsistencyCheckStatement statement = new TableConsistencyCheckStatement("table1", "t2");
DataConsistencyStatementResult result = manager1.createTableCheckSum(statement, null);
assertTrue("Consistency check replicated test ", result.getOk());
// The follower is always back 1, I make an entry to make him apply consistency log
execute(manager1, "INSERT INTO t2.table1 (id,name) values (?,?)", Arrays.asList("3", false));
}
}
try (DBManager manager1 = startDBManager("node1")) {
// restart leader node
manager1.waitForTablespace("t2", 10000);
}
try (DBManager manager1 = startDBManager("node2")) {
// restart follower node
manager1.waitForTablespace("t2", 1000);
}
// Expected 2 call for createChecksum (node1 and node2)
assertEquals(2, callCount.get());
}
Aggregations