use of herddb.model.TransactionResult in project herddb by diennea.
the class TableSpaceManager method rollbackTransaction.
private CompletableFuture<StatementExecutionResult> rollbackTransaction(RollbackTransactionStatement statement, StatementEvaluationContext context) throws StatementExecutionException {
long txId = statement.getTransactionId();
validateTransactionBeforeTxCommand(txId);
LogEntry entry = LogEntryFactory.rollbackTransaction(txId);
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
CommitLogResult pos = log.log(entry, true);
CompletableFuture<StatementExecutionResult> res = pos.logSequenceNumber.thenApplyAsync((lsn) -> {
apply(pos, entry, false);
return new TransactionResult(txId, TransactionResult.OutcomeType.ROLLBACK);
}, callbacksExecutor);
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).thenApply(s -> {
context.setTableSpaceLock(0);
return s;
});
}
return res;
}
use of herddb.model.TransactionResult in project herddb by diennea.
the class ServerSideConnectionPeer method handleTxCommand.
private void handleTxCommand(Pdu message, Channel channel) {
long txId = PduCodec.TxCommand.readTx(message);
int type = PduCodec.TxCommand.readCommand(message);
String tableSpace = PduCodec.TxCommand.readTablespace(message);
TransactionContext transactionContext = new TransactionContext(txId);
Statement statement;
switch(type) {
case TX_COMMAND_COMMIT_TRANSACTION:
statement = new CommitTransactionStatement(tableSpace, txId);
break;
case TX_COMMAND_ROLLBACK_TRANSACTION:
statement = new RollbackTransactionStatement(tableSpace, txId);
break;
case TX_COMMAND_BEGIN_TRANSACTION:
statement = new BeginTransactionStatement(tableSpace);
break;
default:
statement = null;
}
if (statement == null) {
ByteBuf error = PduCodec.ErrorResponse.write(message.messageId, "unknown txcommand type:" + type);
channel.sendReplyMessage(message.messageId, error);
message.close();
} else {
// LOGGER.log(Level.SEVERE, "statement " + statement);
CompletableFuture<StatementExecutionResult> res = server.getManager().executeStatementAsync(statement, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), transactionContext);
// LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", result:" + result);
res.whenComplete((result, err) -> {
try {
if (err != null) {
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 tx command: ", err);
ByteBuf error = composeErrorResponse(message.messageId, err);
channel.sendReplyMessage(message.messageId, error);
}
} else {
if (result instanceof TransactionResult) {
TransactionResult txresult = (TransactionResult) result;
ByteBuf response = PduCodec.TxCommandResult.write(message.messageId, txresult.transactionId);
channel.sendReplyMessage(message.messageId, response);
} else {
ByteBuf error = PduCodec.ErrorResponse.write(message.messageId, "unknown result type:" + result);
channel.sendReplyMessage(message.messageId, error);
}
}
} finally {
message.close();
}
});
}
}
use of herddb.model.TransactionResult in project herddb by diennea.
the class AutocheckPointTest method autoCheckPointTest.
@Test
public void autoCheckPointTest() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Path tmoDir = folder.newFolder("tmoDir").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager(nodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, 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 ,s1 string,n1 int, primary key(k1))", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(1, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(2, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(3, "a", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(4, "a", Integer.valueOf(1234))).getUpdateCount());
manager.checkpoint();
long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "UPDATE tblspace1.tsql set s1='b' where k1=1", Collections.emptyList(), new TransactionContext(tx));
long lastCheckpont = manager.getLastCheckPointTs();
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234", Collections.emptyList(), new TransactionContext(tx))) {
List<DataAccessor> data = scan.consume();
assertEquals(4, data.size());
}
manager.setCheckpointPeriod(1000);
for (int i = 0; i < 100; i++) {
if (lastCheckpont != manager.getLastCheckPointTs()) {
break;
}
Thread.sleep(100);
}
assertNotEquals(lastCheckpont, manager.getLastCheckPointTs());
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234 and s1='b'", Collections.emptyList()).consumeAndClose().size());
}
}
use of herddb.model.TransactionResult in project herddb by diennea.
the class DropTableSQLTest method dropTableWithTransaction.
@Test
public void dropTableWithTransaction() 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);
long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList(), new TransactionContext(tx));
execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), new TransactionContext(tx));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
long tx2 = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
// name is not case sensitive
execute(manager, "DROP TABLE tblspace1.tSQL", Collections.emptyList(), new TransactionContext(tx2));
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
}
execute(manager, "EXECUTE committransaction 'tblspace1'," + tx2, Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
fail();
} catch (TableDoesNotExistException ok) {
assertTrue(manager.getPlanner() instanceof JSQLParserPlanner);
} catch (StatementExecutionException ok) {
assertEquals("From line 1, column 15 to line 1, column 28: Object 'TSQL' not found within 'tblspace1'", ok.getMessage());
assertTrue(manager.getPlanner() instanceof CalcitePlanner);
}
}
}
use of herddb.model.TransactionResult in project herddb by diennea.
the class SimpleRecoveryTest method rollbackDeleteTransactionOnRestart.
@Test
public void rollbackDeleteTransactionOnRestart() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Bytes key = Bytes.from_int(1234);
Bytes value = Bytes.from_long(8888);
Path tmoDir = folder.newFolder("tmoDir").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
InsertStatement insert = new InsertStatement("tblspace1", "t1", new Record(key, value));
assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
// transaction is not committed, and the leader dies, key appear again
long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
DeleteStatement delete = new DeleteStatement("tblspace1", "t1", key, null);
assertEquals(1, manager.executeUpdate(delete, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
{
GetStatement get = new GetStatement("tblspace1", "t1", key, null, false);
GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
assertTrue(result.found());
assertEquals(key, result.getRecord().key);
assertEquals(value, result.getRecord().value);
}
}
}
Aggregations