use of herddb.model.StatementExecutionException in project herddb by diennea.
the class FencingTest method testFencingDuringTransaction.
@Test
public void testFencingDuringTransaction() throws Exception {
ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
try (Server server = new Server(serverconfig_1)) {
server.start();
server.waitForStandaloneBoot();
Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
server.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
StatementExecutionResult executeStatement = server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
long transactionId = executeStatement.transactionId;
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
BookkeeperCommitLog log = (BookkeeperCommitLog) tableSpaceManager.getLog();
long ledgerId = log.getLastSequenceNumber().ledgerId;
assertTrue(ledgerId >= 0);
// we do not want auto-recovery
server.getManager().setActivatorPauseStatus(true);
try (BookKeeper bk = createBookKeeper()) {
try (LedgerHandle fenceLedger = bk.openLedger(ledgerId, BookKeeper.DigestType.CRC32C, "herddb".getBytes(StandardCharsets.UTF_8))) {
}
}
// transaction will continue and see the failure only the time of the commit
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 6)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(transactionId));
try {
server.getManager().executeStatement(new CommitTransactionStatement(TableSpace.DEFAULT, transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
fail();
} catch (StatementExecutionException expected) {
}
while (true) {
System.out.println("status leader:" + tableSpaceManager.isLeader() + " failed:" + tableSpaceManager.isFailed());
if (tableSpaceManager.isFailed()) {
break;
}
Thread.sleep(100);
}
server.getManager().setActivatorPauseStatus(false);
server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
while (true) {
TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
System.out.println("tableSpaceManager_after_failure:" + tableSpaceManager_after_failure);
System.out.println("tableSpaceManager:" + tableSpaceManager);
if (tableSpaceManager_after_failure != null && tableSpaceManager_after_failure != tableSpaceManager) {
break;
}
Thread.sleep(1000);
server.getManager().triggerActivator(ActivatorRunRequest.TABLESPACEMANAGEMENT);
}
TableSpaceManager tableSpaceManager_after_failure = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
Assert.assertNotNull(tableSpaceManager_after_failure);
assertNotSame(tableSpaceManager_after_failure, tableSpaceManager);
assertTrue(!tableSpaceManager_after_failure.isFailed());
server.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(server.getManager(), "select * from t1", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class ForeignKeySQLTest method cannotAlterColumnsWithChildTableRefs.
@Test
public void cannotAlterColumnsWithChildTableRefs() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.parent (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE TABLE tblspace1.child (k2 string primary key,n2 int," + "s2 string, " + "CONSTRAINT fk1 FOREIGN KEY (s2,n2) REFERENCES parent(k1,n1) ON DELETE NO ACTION ON UPDATE NO ACTION)", Collections.emptyList());
Table childTable = manager.getTableSpaceManager("tblspace1").getTableManager("child").getTable();
assertEquals(1, childTable.foreignKeys.length);
StatementExecutionException errCannotDrop = expectThrows(StatementExecutionException.class, () -> {
execute(manager, "DROP TABLE tblspace1.parent", Collections.emptyList());
});
assertEquals("Cannot drop table tblspace1.parent because it has children tables: child", errCannotDrop.getMessage());
StatementExecutionException errCannotDropColumn = expectThrows(StatementExecutionException.class, () -> {
execute(manager, "ALTER TABLE tblspace1.parent DROP COLUMN n1", Collections.emptyList());
});
assertEquals("Cannot drop column parent.n1 because of foreign key constraint fk1 on table child", errCannotDropColumn.getMessage());
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class UpdateTest method updateMultiRowsWithValidationError.
@Test
public void updateMultiRowsWithValidationError() throws Exception {
final int inserts = 10;
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 int, n1 int not null, primary key(k1))", Collections.emptyList());
for (int i = 0; i < inserts; i++) {
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList(Integer.valueOf(i), Integer.valueOf(1))).getUpdateCount());
}
long tx = beginTransaction(manager, "tblspace1");
TransactionContext ctx = new TransactionContext(tx);
// single record failed update
StatementExecutionException error = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
executeUpdate(manager, "UPDATE tblspace1.tsql set n1 = null WHERE n1=1", Collections.emptyList(), ctx);
});
assertEquals("error on column n1 (integer not null):Cannot have null value in non-NULL type integer", error.getMessage());
// multi record failed update
StatementExecutionException errors = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
executeUpdate(manager, "UPDATE tblspace1.tsql set n1 = null", Collections.emptyList(), ctx);
});
assertEquals("error on column n1 (integer not null):Cannot have null value in non-NULL type integer", errors.getMessage());
commitTransaction(manager, "tblspace1", tx);
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class UpdateTest method upsertTest.
@Test
public void upsertTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
List<DataAccessor> recordSet = scan.consumeAndClose();
assertEquals(1, recordSet.size());
assertEquals(1234, recordSet.get(0).get(0));
}
assertEquals(1, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
List<DataAccessor> recordSet = scan.consumeAndClose();
assertEquals(1, recordSet.size());
assertEquals(1235, recordSet.get(0).get(0));
}
assertEquals(4, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1)" + "values(?,?),(?,?),(?,?),(?,?)", Arrays.asList("mykey", Integer.valueOf(1235), "mykey", Integer.valueOf(1236), "mykey", Integer.valueOf(1237), "mykey", Integer.valueOf(1238))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
List<DataAccessor> recordSet = scan.consumeAndClose();
assertEquals(1, recordSet.size());
assertEquals(1238, recordSet.get(0).get(0));
}
assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList()).getUpdateCount());
execute(manager, "ALTER TABLE tblspace1.tsql MODIFY s1 string not null", Collections.emptyList());
// assert that UPSERT fails
StatementExecutionException error = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1235)));
});
assertThat(error.getMessage(), containsString("Column 's1' has no default value and does not allow NULLs"));
// insert a value, n1 has a value
assertEquals(1, executeUpdate(manager, "UPSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,'non-empty')", Arrays.asList("mykey", Integer.valueOf(1235))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
List<DataAccessor> recordSet = scan.consumeAndClose();
assertEquals(1, recordSet.size());
assertEquals(1235, recordSet.get(0).get(0));
}
// upsert, making n1 null now, because it has not been named in the INSERT clause
// use non uppercase casing in UPSERT keyword
assertEquals(1, executeUpdate(manager, "UPsert INTO tblspace1.tsql(k1,s1) values(?,'non-empty')", Arrays.asList("mykey")).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT n1 from tblspace1.tsql where k1=?", Arrays.asList("mykey"))) {
List<DataAccessor> recordSet = scan.consumeAndClose();
assertEquals(1, recordSet.size());
assertNull(recordSet.get(0).get(0));
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TruncateTableSQLTest method truncateTableTransactionTest.
@Test
public void truncateTableTransactionTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE BRIN INDEX test1 ON tblspace1.tsql (k1)", Collections.emptyList());
execute(manager, "CREATE HASH INDEX test2 ON tblspace1.tsql (k1)", Collections.emptyList());
long tx1 = TestUtils.beginTransaction(manager, "tblspace1");
try {
// forbidden, transactions not allowed
execute(manager, "truncate TABLE tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx1));
fail();
} catch (StatementExecutionException ok) {
assertEquals("TRUNCATE TABLE cannot be executed within the context of a Transaction", ok.getMessage());
}
long txId = execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), TransactionContext.AUTOTRANSACTION_TRANSACTION).transactionId;
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList(), new TransactionContext(txId))) {
assertEquals(1, scan.consume().size());
}
try {
// forbidden, a transaction is running on table
execute(manager, "TRUNCATE TABLE tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
fail();
} catch (StatementExecutionException ok) {
assertEquals("TRUNCATE TABLE cannot be executed table tblspace1.tsql: at least one transaction is pending on it", ok.getCause().getMessage());
}
TestUtils.commitTransaction(manager, "tblspace1", txId);
execute(manager, "TRUNCATE TABLE tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(0, scan.consume().size());
}
}
}
Aggregations