use of herddb.model.Transaction in project herddb by diennea.
the class TableManager method executeForeignKeyConstraintsAsParentTable.
private void executeForeignKeyConstraintsAsParentTable(Table childTable, DataAccessor previousValuesOnParentTable, StatementEvaluationContext context, Transaction transaction, boolean delete) throws StatementExecutionException {
// invalidated consistently during DML operations.
for (ForeignKeyDef fk : childTable.foreignKeys) {
String query = parentForeignKeyQueries.computeIfAbsent(childTable.name + "." + fk.name + ".#" + delete, (l -> {
if (fk.onDeleteAction == ForeignKeyDef.ACTION_CASCADE && delete) {
StringBuilder q = new StringBuilder("DELETE FROM ");
q.append(delimit(childTable.tablespace));
q.append(".");
q.append(delimit(childTable.name));
q.append(" WHERE ");
for (int i = 0; i < fk.columns.length; i++) {
if (i > 0) {
q.append(" AND ");
}
q.append(delimit(fk.columns[i]));
q.append("=?");
}
return q.toString();
} else if (fk.onUpdateAction == ForeignKeyDef.ACTION_CASCADE && !delete) {
// the change is more complex, let's keep it for a future work
throw new StatementExecutionException("No supported ON UPDATE CASCADE");
} else if ((fk.onDeleteAction == ForeignKeyDef.ACTION_SETNULL && delete) || (fk.onUpdateAction == ForeignKeyDef.ACTION_SETNULL && !delete)) {
// delete or update it is the same for SET NULL
StringBuilder q = new StringBuilder("UPDATE ");
q.append(delimit(childTable.tablespace));
q.append(".");
q.append(delimit(childTable.name));
q.append(" SET ");
for (int i = 0; i < fk.columns.length; i++) {
if (i > 0) {
q.append(",");
}
q.append(delimit(fk.columns[i]));
q.append("= NULL ");
}
q.append(" WHERE ");
for (int i = 0; i < fk.columns.length; i++) {
if (i > 0) {
q.append(" AND ");
}
q.append(delimit(fk.columns[i]));
q.append("=?");
}
return q.toString();
} else {
// NO ACTION case, check that there is no matching record in the child table that wouble be invalidated
// with '*' we are not going to perform projections or copies
StringBuilder q = new StringBuilder("SELECT * FROM ");
q.append(delimit(childTable.tablespace));
q.append(".");
q.append(delimit(childTable.name));
q.append(" WHERE ");
for (int i = 0; i < fk.columns.length; i++) {
if (i > 0) {
q.append(" AND ");
}
q.append(delimit(fk.columns[i]));
q.append("=?");
}
return q.toString();
}
}));
final List<Object> valuesToMatch = new ArrayList<>(fk.parentTableColumns.length);
for (int i = 0; i < fk.parentTableColumns.length; i++) {
valuesToMatch.add(previousValuesOnParentTable.get(fk.parentTableColumns[i]));
}
TransactionContext tx = transaction != null ? new TransactionContext(transaction.transactionId) : TransactionContext.NO_TRANSACTION;
if (fk.onDeleteAction == ForeignKeyDef.ACTION_CASCADE && delete || fk.onUpdateAction == ForeignKeyDef.ACTION_CASCADE && !delete || fk.onUpdateAction == ForeignKeyDef.ACTION_SETNULL && !delete || fk.onDeleteAction == ForeignKeyDef.ACTION_SETNULL && delete) {
tableSpaceManager.getDbmanager().executeSimpleStatement(tableSpaceManager.getTableSpaceName(), query, valuesToMatch, // every record
-1, // keep read locks in TransactionContext
true, tx, null);
} else {
boolean fkOk;
try (DataScanner scan = tableSpaceManager.getDbmanager().executeSimpleQuery(tableSpaceManager.getTableSpaceName(), query, valuesToMatch, // only one record
1, // keep read locks in TransactionContext
true, tx, null)) {
List<DataAccessor> resultSet = scan.consume();
// we are on the parent side of the relation
// we are okay if there is no matching record
// TODO: return the list of PKs in order to implement CASCADE operations
fkOk = resultSet.isEmpty();
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
if (!fkOk) {
throw new ForeignKeyViolationException(fk.name, "foreignKey " + childTable.name + "." + fk.name + " violated");
}
}
}
}
use of herddb.model.Transaction in project herddb by diennea.
the class TableManager method executeDeleteAsync.
private CompletableFuture<StatementExecutionResult> executeDeleteAsync(DeleteStatement delete, Transaction transaction, StatementEvaluationContext context) {
AtomicInteger updateCount = new AtomicInteger();
Holder<Bytes> lastKey = new Holder<>();
Holder<Bytes> lastValue = new Holder<>();
long transactionId = transaction != null ? transaction.transactionId : 0;
Predicate predicate = delete.getPredicate();
List<CompletableFuture<PendingLogEntryWork>> writes = new ArrayList<>();
Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(table.name);
ScanStatement scan = new ScanStatement(table.tablespace, table, predicate);
try {
accessTableData(scan, context, new ScanResultOperation() {
@Override
public void accept(Record current, LockHandle lockHandle) throws StatementExecutionException, LogNotAvailableException, DataStorageManagerException {
// ensure we are holding the write locks on every unique index
List<UniqueIndexLockReference> uniqueIndexes = null;
try {
if (indexes != null || childrenTables != null) {
DataAccessor dataAccessor = current.getDataAccessor(table);
if (childrenTables != null) {
for (Table childTable : childrenTables) {
executeForeignKeyConstraintsAsParentTable(childTable, dataAccessor, context, transaction, true);
}
}
if (indexes != null) {
for (AbstractIndexManager index : indexes.values()) {
if (index.isUnique()) {
Bytes indexKey = RecordSerializer.serializeIndexKey(dataAccessor, index.getIndex(), index.getColumnNames());
if (uniqueIndexes == null) {
uniqueIndexes = new ArrayList<>(1);
}
UniqueIndexLockReference uniqueIndexLock = new UniqueIndexLockReference(index, indexKey);
uniqueIndexes.add(uniqueIndexLock);
LockHandle lockForIndex = lockForWrite(uniqueIndexLock.key, transaction, index.getIndexName(), index.getLockManager());
if (transaction == null) {
uniqueIndexLock.lockHandle = lockForIndex;
}
}
}
}
}
} catch (IllegalArgumentException | herddb.utils.IllegalDataAccessException | StatementExecutionException err) {
locksManager.releaseLock(lockHandle);
StatementExecutionException finalError;
if (!(err instanceof StatementExecutionException)) {
finalError = new StatementExecutionException(err.getMessage(), err);
} else {
finalError = (StatementExecutionException) err;
}
CompletableFuture<PendingLogEntryWork> res = Futures.exception(finalError);
if (uniqueIndexes != null) {
for (UniqueIndexLockReference lock : uniqueIndexes) {
res = releaseWriteLock(res, lockHandle, lock.indexManager.getLockManager());
}
}
writes.add(res);
return;
}
LogEntry entry = LogEntryFactory.delete(table, current.key, transaction);
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
final List<UniqueIndexLockReference> _uniqueIndexes = uniqueIndexes;
writes.add(pos.logSequenceNumber.thenApply(lsn -> new PendingLogEntryWork(entry, pos, lockHandle, _uniqueIndexes)));
lastKey.value = current.key;
lastValue.value = current.value;
updateCount.incrementAndGet();
}
}, transaction, true, true);
} catch (HerdDBInternalException err) {
LOGGER.log(Level.SEVERE, "bad error during a delete", err);
return Futures.exception(err);
}
if (writes.isEmpty()) {
return CompletableFuture.completedFuture(new DMLStatementExecutionResult(transactionId, 0, null, null));
}
if (writes.size() == 1) {
return writes.get(0).whenCompleteAsync((pending, error) -> {
try {
// apply any of the DML operations
if (error == null) {
apply(pending.pos, pending.entry, false);
}
} finally {
releaseMultiplePendingLogEntryWorks(writes);
}
}, tableSpaceManager.getCallbacksExecutor()).thenApply((pending) -> {
return new DMLStatementExecutionResult(transactionId, updateCount.get(), lastKey.value, delete.isReturnValues() ? lastValue.value : null);
});
} else {
return Futures.collect(writes).whenCompleteAsync((pendings, error) -> {
try {
// apply any of the DML operations
if (error == null) {
for (PendingLogEntryWork pending : pendings) {
apply(pending.pos, pending.entry, false);
}
}
} finally {
releaseMultiplePendingLogEntryWorks(writes);
}
}, tableSpaceManager.getCallbacksExecutor()).thenApply((pendings) -> {
return new DMLStatementExecutionResult(transactionId, updateCount.get(), lastKey.value, delete.isReturnValues() ? lastValue.value : null);
});
}
}
use of herddb.model.Transaction in project herddb by diennea.
the class TableSpaceManager method apply.
void apply(CommitLogResult position, LogEntry entry, boolean recovery) throws DataStorageManagerException, DDLException {
if (!position.deferred || position.sync) {
// this will wait for the write to be acknowledged by the log
// it can throw LogNotAvailableException
this.actualLogSequenceNumber = position.getLogSequenceNumber();
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "apply {0} {1}", new Object[] { position.getLogSequenceNumber(), entry });
}
} else {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "apply {0} {1}", new Object[] { position, entry });
}
}
switch(entry.type) {
case LogEntryType.NOOP:
{
// NOOP
}
break;
case LogEntryType.BEGINTRANSACTION:
{
long id = entry.transactionId;
Transaction transaction = new Transaction(id, tableSpaceName, position);
transactions.put(id, transaction);
}
break;
case LogEntryType.ROLLBACKTRANSACTION:
{
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
if (transaction == null) {
throw new DataStorageManagerException("invalid transaction id " + id + ", only " + transactions.keySet());
}
List<AbstractIndexManager> indexManagers = new ArrayList<>(indexes.values());
for (AbstractIndexManager indexManager : indexManagers) {
if (indexManager.getCreatedInTransaction() == 0 || indexManager.getCreatedInTransaction() == id) {
indexManager.onTransactionRollback(transaction);
}
}
List<AbstractTableManager> managers = new ArrayList<>(tables.values());
for (AbstractTableManager manager : managers) {
if (manager.getCreatedInTransaction() == 0 || manager.getCreatedInTransaction() == id) {
Table table = manager.getTable();
if (transaction.isNewTable(table.name)) {
LOGGER.log(Level.INFO, "rollback CREATE TABLE " + table.tablespace + "." + table.name);
disposeTable(manager);
Map<String, AbstractIndexManager> indexes = indexesByTable.remove(manager.getTable().name);
if (indexes != null) {
for (AbstractIndexManager indexManager : indexes.values()) {
disposeIndexManager(indexManager);
}
}
} else {
manager.onTransactionRollback(transaction);
}
}
}
transactions.remove(transaction.transactionId);
}
break;
case LogEntryType.COMMITTRANSACTION:
{
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
if (transaction == null) {
throw new DataStorageManagerException("invalid transaction id " + id);
}
LogSequenceNumber commit = position.getLogSequenceNumber();
transaction.sync(commit);
List<AbstractTableManager> managers = new ArrayList<>(tables.values());
for (AbstractTableManager manager : managers) {
if (manager.getCreatedInTransaction() == 0 || manager.getCreatedInTransaction() == id) {
manager.onTransactionCommit(transaction, recovery);
}
}
List<AbstractIndexManager> indexManagers = new ArrayList<>(indexes.values());
for (AbstractIndexManager indexManager : indexManagers) {
if (indexManager.getCreatedInTransaction() == 0 || indexManager.getCreatedInTransaction() == id) {
indexManager.onTransactionCommit(transaction, recovery);
}
}
if ((transaction.droppedTables != null && !transaction.droppedTables.isEmpty()) || (transaction.droppedIndexes != null && !transaction.droppedIndexes.isEmpty())) {
if (transaction.droppedTables != null) {
for (String dropped : transaction.droppedTables) {
for (AbstractTableManager manager : managers) {
if (manager.getTable().name.equals(dropped)) {
disposeTable(manager);
}
}
}
}
if (transaction.droppedIndexes != null) {
for (String dropped : transaction.droppedIndexes) {
for (AbstractIndexManager manager : indexManagers) {
if (manager.getIndex().name.equals(dropped)) {
disposeIndexManager(manager);
}
}
}
}
}
if ((transaction.newTables != null && !transaction.newTables.isEmpty()) || (transaction.droppedTables != null && !transaction.droppedTables.isEmpty()) || (transaction.newIndexes != null && !transaction.newIndexes.isEmpty()) || (transaction.droppedIndexes != null && !transaction.droppedIndexes.isEmpty())) {
writeTablesOnDataStorageManager(position, false);
dbmanager.getPlanner().clearCache();
}
transactions.remove(transaction.transactionId);
}
break;
case LogEntryType.CREATE_TABLE:
{
Table table = Table.deserialize(entry.value.to_array());
if (entry.transactionId > 0) {
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
transaction.registerNewTable(table, position);
}
bootTable(table, entry.transactionId, null, true);
if (entry.transactionId <= 0) {
writeTablesOnDataStorageManager(position, false);
}
}
break;
case LogEntryType.CREATE_INDEX:
{
Index index = Index.deserialize(entry.value.to_array());
if (entry.transactionId > 0) {
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
transaction.registerNewIndex(index, position);
}
AbstractTableManager tableManager = tables.get(index.table);
if (tableManager == null) {
throw new RuntimeException("table " + index.table + " does not exists");
}
bootIndex(index, tableManager, true, entry.transactionId, true, false);
if (entry.transactionId <= 0) {
writeTablesOnDataStorageManager(position, false);
}
}
break;
case LogEntryType.DROP_TABLE:
{
String tableName = entry.tableName;
if (entry.transactionId > 0) {
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
transaction.registerDropTable(tableName, position);
} else {
AbstractTableManager manager = tables.get(tableName);
if (manager != null) {
disposeTable(manager);
Map<String, AbstractIndexManager> indexes = indexesByTable.get(tableName);
if (indexes != null && !indexes.isEmpty()) {
LOGGER.log(Level.SEVERE, "It looks like we are dropping a table " + tableName + " with these indexes " + indexes);
}
}
}
if (entry.transactionId <= 0) {
writeTablesOnDataStorageManager(position, false);
}
}
break;
case LogEntryType.DROP_INDEX:
{
String indexName = entry.value.to_string();
if (entry.transactionId > 0) {
long id = entry.transactionId;
Transaction transaction = transactions.get(id);
transaction.registerDropIndex(indexName, position);
} else {
AbstractIndexManager manager = indexes.get(indexName);
if (manager != null) {
disposeIndexManager(manager);
}
}
if (entry.transactionId <= 0) {
writeTablesOnDataStorageManager(position, false);
dbmanager.getPlanner().clearCache();
}
}
break;
case LogEntryType.ALTER_TABLE:
{
Table table = Table.deserialize(entry.value.to_array());
alterTable(table, null);
writeTablesOnDataStorageManager(position, false);
}
break;
case LogEntryType.TABLE_CONSISTENCY_CHECK:
{
/*
In recovery mode, we need to skip the consistency check.
The tablespace may not be avaible yet and therefore calcite will not able to performed the select query.
*/
if (recovery) {
LOGGER.log(Level.INFO, "skip {0} consistency check LogEntry {1}", new Object[] { tableSpaceName, entry });
break;
}
try {
TableChecksum check = MAPPER.readValue(entry.value.to_array(), TableChecksum.class);
String tableSpace = check.getTableSpaceName();
String query = check.getQuery();
String tableName = entry.tableName;
// In the entry type = 14, the follower will have to run the query on the transaction log
if (!isLeader()) {
AbstractTableManager tablemanager = this.getTableManager(tableName);
DBManager manager = this.getDbmanager();
if (tablemanager == null || tablemanager.getCreatedInTransaction() > 0) {
throw new TableDoesNotExistException(String.format("Table %s does not exist.", tablemanager));
}
/*
scan = true
allowCache = false
returnValues = false
maxRows = -1
*/
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, query, Collections.emptyList(), true, false, false, -1);
TableChecksum scanResult = TableDataChecksum.createChecksum(manager, translated, this, tableSpace, tableName);
long followerDigest = scanResult.getDigest();
long leaderDigest = check.getDigest();
long leaderNumRecords = check.getNumRecords();
long followerNumRecords = scanResult.getNumRecords();
// the necessary condition to pass the check is to have exactly the same digest and the number of records processed
if (followerDigest == leaderDigest && leaderNumRecords == followerNumRecords) {
LOGGER.log(Level.INFO, "Data consistency check PASS for table {0} tablespace {1} with Checksum {2}", new Object[] { tableName, tableSpace, followerDigest });
} else {
LOGGER.log(Level.SEVERE, "Data consistency check FAILED for table {0} in tablespace {1} with Checksum {2}", new Object[] { tableName, tableSpace, followerDigest });
}
} else {
long digest = check.getDigest();
LOGGER.log(Level.INFO, "Created checksum {0} for table {1} in tablespace {2} on node {3}", new Object[] { digest, entry.tableName, tableSpace, this.getDbmanager().getNodeId() });
}
} catch (IOException | DataScannerException ex) {
LOGGER.log(Level.SEVERE, "Error during table consistency check ", ex);
}
}
break;
default:
// other entry types are not important for the tablespacemanager
break;
}
if (entry.tableName != null && entry.type != LogEntryType.CREATE_TABLE && entry.type != LogEntryType.CREATE_INDEX && entry.type != LogEntryType.ALTER_TABLE && entry.type != LogEntryType.DROP_TABLE && entry.type != LogEntryType.TABLE_CONSISTENCY_CHECK) {
AbstractTableManager tableManager = tables.get(entry.tableName);
tableManager.apply(position, entry, recovery);
}
}
use of herddb.model.Transaction in project herddb by diennea.
the class TableSpaceManager method executeTableAwareStatement.
private CompletableFuture<StatementExecutionResult> executeTableAwareStatement(Statement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
TableAwareStatement st = (TableAwareStatement) statement;
String table = st.getTable();
AbstractTableManager manager = tables.get(table);
CompletableFuture<StatementExecutionResult> res;
if (manager == null) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName));
} else if (manager.getCreatedInTransaction() > 0 && (transaction == null || transaction.transactionId != manager.getCreatedInTransaction())) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + manager.getCreatedInTransaction()));
} else {
res = manager.executeStatementAsync(statement, transaction, context);
}
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).whenComplete((s, err) -> {
context.setTableSpaceLock(0);
});
}
return res;
}
use of herddb.model.Transaction in project herddb by diennea.
the class TableSpaceManager method restoreRawDumpedTransactions.
public void restoreRawDumpedTransactions(List<Transaction> entries) {
for (Transaction ld : entries) {
LOGGER.log(Level.INFO, "restore transaction " + ld);
transactions.put(ld.transactionId, ld);
}
}
Aggregations