Skip to content

Commit

Permalink
[C#] Recovery fixes (#421)
Browse files Browse the repository at this point in the history
* [C#] Fix recovery of hash tables, speed up snapshot recovery.

Use snapshot as default checkpoint for benchmarks.

* avoid unnecessary hlog read.

* ensure tail page allocated after recovery
  • Loading branch information
badrishc authored Mar 8, 2021
1 parent 624a566 commit 1eca35c
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cs/benchmark/FasterYcsbBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public FASTER_YcsbBenchmark(int threadCount_, int numaStyle_, string distributio

if (kSmallMemoryLog)
store = new FasterKV<Key, Value>
(kMaxKey / 2, new LogSettings { LogDevice = device, PreallocateLog = true, PageSizeBits = 22, SegmentSizeBits = 26, MemorySizeBits = 26 }, new CheckpointSettings { CheckPointType = CheckpointType.FoldOver, CheckpointDir = path });
(kMaxKey / 2, new LogSettings { LogDevice = device, PreallocateLog = true, PageSizeBits = 22, SegmentSizeBits = 26, MemorySizeBits = 26 }, new CheckpointSettings { CheckPointType = CheckpointType.Snapshot, CheckpointDir = path });
else
store = new FasterKV<Key, Value>
(kMaxKey / 2, new LogSettings { LogDevice = device, PreallocateLog = true }, new CheckpointSettings { CheckPointType = CheckpointType.FoldOver, CheckpointDir = path });
(kMaxKey / 2, new LogSettings { LogDevice = device, PreallocateLog = true }, new CheckpointSettings { CheckPointType = CheckpointType.Snapshot, CheckpointDir = path });
}

private void RunYcsb(int thread_idx)
Expand Down
11 changes: 7 additions & 4 deletions cs/src/core/Allocator/AllocatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,12 +1176,15 @@ public void RecoveryReset(long tailAddress, long headAddress, long beginAddress,
TailPageOffset.Page = (int)tailPage;
TailPageOffset.Offset = (int)offsetInPage;

// allocate next page as well - this is an invariant in the allocator!
// Allocate current page if necessary
var pageIndex = (TailPageOffset.Page % BufferSize);
if (!IsAllocated(pageIndex))
AllocatePage(pageIndex);

// Allocate next page as well - this is an invariant in the allocator!
var nextPageIndex = (pageIndex + 1) % BufferSize;
if (tailAddress > 0)
if (!IsAllocated(nextPageIndex))
AllocatePage(nextPageIndex);
if (!IsAllocated(nextPageIndex))
AllocatePage(nextPageIndex);

BeginAddress = beginAddress;
HeadAddress = headAddress;
Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Allocator/BlittableAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal override void AllocatePage(int index)

handles[index] = GCHandle.Alloc(tmp, GCHandleType.Pinned);
long p = (long)handles[index].AddrOfPinnedObject();
pointers[index] = (p + (sectorSize - 1)) & ~(sectorSize - 1);
pointers[index] = (p + (sectorSize - 1)) & ~((long)sectorSize - 1);
values[index] = tmp;
}

Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Allocator/BlittableFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Allocate(int index)

handles[index] = GCHandle.Alloc(tmp, GCHandleType.Pinned);
long p = (long)handles[index].AddrOfPinnedObject();
pointers[index] = (p + (sectorSize - 1)) & ~(sectorSize - 1);
pointers[index] = (p + (sectorSize - 1)) & ~((long)sectorSize - 1);
frame[index] = tmp;
}

Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Allocator/GenericAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ private void AsyncReadPageWithObjectsCallback<TContext>(uint errorCode, uint num
if (size > int.MaxValue)
throw new FasterException("Unable to read object page, total size greater than 2GB: " + size);

var alignedLength = (size + (sectorSize - 1)) & ~(sectorSize - 1);
var alignedLength = (size + (sectorSize - 1)) & ~((long)sectorSize - 1);
var objBuffer = bufferPool.Get((int)alignedLength);
result.freeBuffer2 = objBuffer;

Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Allocator/VarLenBlittableAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ internal override void AllocatePage(int index)

handles[index] = GCHandle.Alloc(tmp, GCHandleType.Pinned);
long p = (long)handles[index].AddrOfPinnedObject();
pointers[index] = (p + (sectorSize - 1)) & ~(sectorSize - 1);
pointers[index] = (p + (sectorSize - 1)) & ~((long)sectorSize - 1);
values[index] = tmp;
}

Expand Down
4 changes: 2 additions & 2 deletions cs/src/core/Index/Recovery/IndexCheckpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal unsafe void TakeIndexFuzzyCheckpoint()
BeginMainIndexCheckpoint(ht_version, _indexCheckpoint.main_ht_device, out ulong ht_num_bytes_written, UseReadCache, SkipReadCacheBucket);

var sectorSize = _indexCheckpoint.main_ht_device.SectorSize;
var alignedIndexSize = (uint)((ht_num_bytes_written + (sectorSize - 1)) & ~(sectorSize - 1));
var alignedIndexSize = (ht_num_bytes_written + (sectorSize - 1)) & ~((ulong)sectorSize - 1);
overflowBucketsAllocator.BeginCheckpoint(_indexCheckpoint.main_ht_device, alignedIndexSize, out ulong ofb_num_bytes_written, UseReadCache, SkipReadCacheBucket, epoch);
_indexCheckpoint.info.num_ht_bytes = ht_num_bytes_written;
_indexCheckpoint.info.num_ofb_bytes = ofb_num_bytes_written;
Expand All @@ -37,7 +37,7 @@ internal void TakeIndexFuzzyCheckpoint(int ht_version, IDevice device,
{
BeginMainIndexCheckpoint(ht_version, device, out numBytesWritten);
var sectorSize = device.SectorSize;
var alignedIndexSize = (uint)((numBytesWritten + (sectorSize - 1)) & ~(sectorSize - 1));
var alignedIndexSize = (numBytesWritten + (sectorSize - 1)) & ~((ulong)sectorSize - 1);
overflowBucketsAllocator.BeginCheckpoint(ofbdevice, alignedIndexSize, out ofbnumBytesWritten);
num_ofb_buckets = overflowBucketsAllocator.GetMaxValidAddress();
}
Expand Down
12 changes: 6 additions & 6 deletions cs/src/core/Index/Recovery/IndexRecovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class FasterBase
// Derived class exposed API
internal void RecoverFuzzyIndex(IndexCheckpointInfo info)
{
uint alignedIndexSize = InitializeMainIndexRecovery(ref info, isAsync: false);
ulong alignedIndexSize = InitializeMainIndexRecovery(ref info, isAsync: false);
overflowBucketsAllocator.Recover(info.main_ht_device, alignedIndexSize, info.info.num_buckets, info.info.num_ofb_bytes);

// Wait until reading is complete
Expand All @@ -28,13 +28,13 @@ internal void RecoverFuzzyIndex(IndexCheckpointInfo info)

internal async ValueTask RecoverFuzzyIndexAsync(IndexCheckpointInfo info, CancellationToken cancellationToken)
{
uint alignedIndexSize = InitializeMainIndexRecovery(ref info, isAsync: true);
ulong alignedIndexSize = InitializeMainIndexRecovery(ref info, isAsync: true);
await this.recoveryCountdown.WaitAsync(cancellationToken);
await overflowBucketsAllocator.RecoverAsync(info.main_ht_device, alignedIndexSize, info.info.num_buckets, info.info.num_ofb_bytes, cancellationToken);
FinalizeMainIndexRecovery(info);
}

private uint InitializeMainIndexRecovery(ref IndexCheckpointInfo info, bool isAsync)
private ulong InitializeMainIndexRecovery(ref IndexCheckpointInfo info, bool isAsync)
{
var token = info.info.token;
var ht_version = resizeInfo.version;
Expand All @@ -53,7 +53,7 @@ private uint InitializeMainIndexRecovery(ref IndexCheckpointInfo info, bool isAs
BeginMainIndexRecovery(ht_version, info.main_ht_device, info.info.num_ht_bytes, isAsync);


var alignedIndexSize = (uint)((info.info.num_ht_bytes + (sectorSize - 1)) & ~(sectorSize - 1));
var alignedIndexSize = (info.info.num_ht_bytes + (sectorSize - 1)) & ~((ulong)sectorSize - 1);
return alignedIndexSize;
}

Expand All @@ -71,7 +71,7 @@ internal void RecoverFuzzyIndex(int ht_version, IDevice device, ulong num_ht_byt
{
BeginMainIndexRecovery(ht_version, device, num_ht_bytes);
var sectorSize = device.SectorSize;
var alignedIndexSize = (uint)((num_ht_bytes + (sectorSize - 1)) & ~(sectorSize - 1));
var alignedIndexSize = (num_ht_bytes + (sectorSize - 1)) & ~((ulong)sectorSize - 1);
overflowBucketsAllocator.Recover(ofbdevice, alignedIndexSize, num_buckets, num_ofb_bytes);
}

Expand All @@ -81,7 +81,7 @@ internal async ValueTask RecoverFuzzyIndexAsync(int ht_version, IDevice device,
BeginMainIndexRecovery(ht_version, device, num_ht_bytes, isAsync: true);
await this.recoveryCountdown.WaitAsync(cancellationToken);
var sectorSize = device.SectorSize;
var alignedIndexSize = (uint)((num_ht_bytes + (sectorSize - 1)) & ~(sectorSize - 1));
var alignedIndexSize = (num_ht_bytes + (sectorSize - 1)) & ~((ulong)sectorSize - 1);
await overflowBucketsAllocator.RecoverAsync(ofbdevice, alignedIndexSize, num_buckets, num_ofb_bytes, cancellationToken);
}

Expand Down
Loading

0 comments on commit 1eca35c

Please sign in to comment.