Skip to content

Commit

Permalink
Adds methods to clear stale transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed Jul 23, 2022
1 parent 44c2881 commit c1f680c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/ZoneTree/ITransactionalZoneTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,4 @@ public interface ITransactionalZoneTree<TKey, TValue> : IDisposable
/// </summary>
/// <param name="key">Key</param>
void DeleteAutoCommit(in TKey key);

/// <summary>
/// Rollbacks all uncommitted transactions.
/// </summary>
int RollbackAllUncommitted();
}
14 changes: 12 additions & 2 deletions src/ZoneTree/ITransactionalZoneTreeMaintenance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ public interface ITransactionalZoneTreeMaintenance<TKey, TValue>
void DestroyTree();

/// <summary>
/// Aborts and rollback all uncommitted transactions.
/// Rollbacks all uncommitted transactions.
/// </summary>
void RollbackUncommittedTransactions();
/// <returns>Count of rollbacked transactions.</returns>
int RollbackAllUncommitted();

/// <summary>
/// Rollbacks all uncommitted transaction ids started before given date-time.
/// Transaction log memory usage increases by state uncommitted transaction ids.
/// Those must be rollbacked.
/// </summary>
/// <param name="dateTime">Max start time (exclusive)</param>
/// <returns>Count of rollbacked transactions.</returns>
int RollbackUncommittedTransactionIdsBefore(DateTime dateTime);
}
6 changes: 3 additions & 3 deletions src/ZoneTree/IZoneTreeMaintenance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,21 @@ public interface IZoneTreeMaintenance<TKey, TValue>
event CanNotDropReadOnlySegment<TKey, TValue> OnCanNotDropReadOnlySegment;

/// <summary>
/// Event is fired when a write ahead log cannot be dropped.
/// Event is fired when a disk segment cannot be dropped.
/// This does not harm the database consistency.
/// The cleanup task can be done later.
/// </summary>
event CanNotDropDiskSegment<TKey, TValue> OnCanNotDropDiskSegment;

/// <summary>
/// Event is fired when a write ahead log cannot be dropped.
/// Event is fired when a disk segment creator cannot be dropped.
/// This does not harm the database consistency.
/// The cleanup task can be done later.
/// </summary>
event CanNotDropDiskSegmentCreator<TKey, TValue> OnCanNotDropDiskSegmentCreator;

/// <summary>
/// Event is fired the ZoneTree is disposing.
/// Event is fired when the ZoneTree is disposing.
/// </summary>
event ZoneTreeIsDisposing<TKey, TValue> OnZoneTreeIsDisposing;
}
Expand Down
14 changes: 14 additions & 0 deletions src/ZoneTree/Transactional/BasicTransactionLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,18 @@ private void DeleteTransactionFromMemory(long id)
HistoryTable.TryDeleteFromMemory(id);
DependencyTable.TryDeleteFromMemory(id);
}

public IReadOnlyList<long> GetUncommittedTransactionIdsBefore(DateTime dateTime)
{
var ticks = dateTime.Ticks;
lock (this)
{
var transactionIds = Transactions.Keys;
return TransactionIds.Where(x =>
Transactions.TryGetValue(x, out var meta)
&& meta.State == TransactionState.Uncommitted
&& meta.StartedAt < ticks)
.ToArray();
}
}
}
17 changes: 17 additions & 0 deletions src/ZoneTree/Transactional/ITransactionLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@ public interface ITransactionLog<TKey, TValue> : IDisposable
/// </summary>
int CompactionThreshold { get; set; }

/// <summary>
/// Transaction count in the memory.
/// </summary>
int TransactionCount { get; }

/// <summary>
/// Return all transaction ids that are available in memory.
/// </summary>
IReadOnlyList<long> TransactionIds { get; }

/// <summary>
/// Returns all uncommitted transaction ids.
/// </summary>
IReadOnlyList<long> UncommittedTransactionIds { get; }

/// <summary>
/// Retrieves all uncommitted transaction ids started before given date-time.
/// This method can be used to gather stale uncommitted transaction ids.
/// </summary>
/// <param name="dateTime">Max start time (exclusive)</param>
/// <returns>Uncommitted ids</returns>
IReadOnlyList<long> GetUncommittedTransactionIdsBefore(DateTime dateTime);

long GetNextTransactionId();

void TransactionStarted(long transactionId);
Expand Down
12 changes: 12 additions & 0 deletions src/ZoneTree/Transactional/OptimisticZoneTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,16 @@ public int RollbackAllUncommitted()
}
return count;
}

public int RollbackUncommittedTransactionIdsBefore(DateTime dateTime)
{
var count = 0;
var uncommitted = TransactionLog.GetUncommittedTransactionIdsBefore(dateTime);
foreach (var u in uncommitted)
{
Rollback(u);
++count;
}
return count;
}
}

0 comments on commit c1f680c

Please sign in to comment.