Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change all messages to pass back their checksum. Switch to callback style #73015

Merged
merged 6 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Workspaces/CoreTestUtilities/Fakes/SimpleAssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ namespace Microsoft.CodeAnalysis.Remote.Testing;
internal sealed class SimpleAssetSource(ISerializerService serializerService, IReadOnlyDictionary<Checksum, object> map) : IAssetSource
{
public ValueTask GetAssetsAsync<T, TArg>(
Checksum solutionChecksum, AssetPath assetPath, ReadOnlyMemory<Checksum> checksums, ISerializerService deserializerService, Action<int, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
Checksum solutionChecksum, AssetPath assetPath, ReadOnlyMemory<Checksum> checksums, ISerializerService deserializerService, Action<Checksum, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
{
var index = 0;
foreach (var checksum in checksums.Span)
{
Contract.ThrowIfFalse(map.TryGetValue(checksum, out var data));
Expand All @@ -38,8 +37,7 @@ public ValueTask GetAssetsAsync<T, TArg>(
using var reader = ObjectReader.GetReader(stream, leaveOpen: true, cancellationToken);
var asset = deserializerService.Deserialize(data.GetWellKnownSynchronizationKind(), reader, cancellationToken);
Contract.ThrowIfNull(asset);
callback(index, (T)asset, arg);
index++;
callback(checksum, (T)asset, arg);
}

return ValueTaskFactory.CompletedTask;
Expand Down
31 changes: 12 additions & 19 deletions src/Workspaces/Remote/Core/RemoteHostAssetSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,25 @@ public static async ValueTask WriteDataAsync(

Debug.Assert(assetMap != null);

for (var i = 0; i < checksums.Span.Length; i++)
foreach (var (checksum, asset) in assetMap)
{
var checksum = checksums.Span[i];
var asset = assetMap[checksum];
Contract.ThrowIfNull(asset);

var kind = asset.GetWellKnownSynchronizationKind();
checksum.WriteTo(writer);
writer.WriteInt32((int)kind);
serializer.Serialize(asset, writer, context, cancellationToken);

// We flush after each item as that forms a reasonably sized chunk of data to want to then send over the
// pipe for the reader on the other side to read. This allows the item-writing to remain entirely
// synchronous without any blocking on async flushing, while also ensuring that we're not buffering the
// entire stream of data into the pipe before it gets sent to the other side.
WriteAsset(writer, serializer, context, asset, cancellationToken);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inliend this method.

await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
}

return;

static void WriteAsset(ObjectWriter writer, ISerializerService serializer, SolutionReplicationContext context, object asset, CancellationToken cancellationToken)
{
Contract.ThrowIfNull(asset);
var kind = asset.GetWellKnownSynchronizationKind();
writer.WriteInt32((int)kind);

serializer.Serialize(asset, writer, context, cancellationToken);
}
}

public static ValueTask ReadDataAsync<T, TArg>(
PipeReader pipeReader, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<int, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
PipeReader pipeReader, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<Checksum, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
{
// Suppress ExecutionContext flow for asynchronous operations operate on the pipe. In addition to avoiding
// ExecutionContext allocations, this clears the LogicalCallContext and avoids the need to clone data set by
Expand All @@ -77,15 +69,15 @@ public static ValueTask ReadDataAsync<T, TArg>(
return ReadDataSuppressedFlowAsync(pipeReader, solutionChecksum, objectCount, serializerService, callback, arg, cancellationToken);

static async ValueTask ReadDataSuppressedFlowAsync(
PipeReader pipeReader, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<int, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
PipeReader pipeReader, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<Checksum, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
{
using var stream = await pipeReader.AsPrebufferedStreamAsync(cancellationToken).ConfigureAwait(false);
ReadData(stream, solutionChecksum, objectCount, serializerService, callback, arg, cancellationToken);
}
}

public static void ReadData<T, TArg>(
Stream stream, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<int, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
Stream stream, Checksum solutionChecksum, int objectCount, ISerializerService serializerService, Action<Checksum, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
{
using var reader = ObjectReader.GetReader(stream, leaveOpen: true, cancellationToken);

Expand All @@ -96,12 +88,13 @@ public static void ReadData<T, TArg>(

for (int i = 0, n = objectCount; i < n; i++)
{
var checksum = Checksum.ReadFrom(reader);
var kind = (WellKnownSynchronizationKind)reader.ReadInt32();

// in service hub, cancellation means simply closed stream
var result = serializerService.Deserialize(kind, reader, cancellationToken);
Contract.ThrowIfNull(result);
callback(i, (T)result, arg);
callback(checksum, (T)result, arg);
}
}
}
Expand Down
35 changes: 13 additions & 22 deletions src/Workspaces/Remote/ServiceHub/Host/AssetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,26 @@ private async ValueTask SynchronizeAssetsAsync<T, TArg>(
if (missingChecksumsCount > 0)
{
var missingChecksumsMemory = new ReadOnlyMemory<Checksum>(missingChecksums, 0, missingChecksumsCount);
Contract.ThrowIfTrue(missingChecksumsMemory.Length == 0);

await RequestAssetsAsync(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inlined this method call.

assetPath, missingChecksumsMemory,
#if NETCOREAPP
Contract.ThrowIfTrue(missingChecksumsMemory.Span.Contains(Checksum.Null));
#else
Contract.ThrowIfTrue(missingChecksumsMemory.Span.IndexOf(Checksum.Null) >= 0);
#endif

await _assetSource.GetAssetsAsync(
_solutionChecksum, assetPath, missingChecksumsMemory, _serializerService,
static (
int index,
Checksum missingChecksum,
T missingAsset,
(AssetProvider assetProvider, Checksum[] missingChecksums, Action<Checksum, T, TArg>? callback, TArg? arg) tuple) =>
{
var missingChecksum = tuple.missingChecksums[index];
// Add to cache, returning the existing asset if it was added by another thread.
missingAsset = (T)tuple.assetProvider._assetCache.GetOrAdd(missingChecksum, missingAsset!);

// Let our caller know about the asset if they're asking for it.
tuple.callback?.Invoke(missingChecksum, missingAsset, tuple.arg!);
tuple.assetProvider._assetCache.GetOrAdd(missingChecksum, missingAsset!);
},
(this, missingChecksums, callback, arg),
cancellationToken).ConfigureAwait(false);
Expand All @@ -307,22 +315,5 @@ await RequestAssetsAsync(
s_checksumPool.Free(missingChecksums);
}
}

return;
}

private async ValueTask RequestAssetsAsync<T, TArg>(
AssetPath assetPath, ReadOnlyMemory<Checksum> checksums, Action<int, T, TArg> callback, TArg arg, CancellationToken cancellationToken)
{
#if NETCOREAPP
Contract.ThrowIfTrue(checksums.Span.Contains(Checksum.Null));
#else
Contract.ThrowIfTrue(checksums.Span.IndexOf(Checksum.Null) >= 0);
#endif

if (checksums.Length == 0)
return;

await _assetSource.GetAssetsAsync(_solutionChecksum, assetPath, checksums, _serializerService, callback, arg, cancellationToken).ConfigureAwait(false);
}
}
2 changes: 1 addition & 1 deletion src/Workspaces/Remote/ServiceHub/Host/IAssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ValueTask GetAssetsAsync<T, TArg>(
AssetPath assetPath,
ReadOnlyMemory<Checksum> checksums,
ISerializerService serializerService,
Action<int, T, TArg> callback,
Action<Checksum, T, TArg> callback,
TArg arg,
CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async ValueTask GetAssetsAsync<T, TArg>(
AssetPath assetPath,
ReadOnlyMemory<Checksum> checksums,
ISerializerService serializerService,
Action<int, T, TArg> assetCallback,
Action<Checksum, T, TArg> assetCallback,
TArg arg,
CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -95,7 +92,7 @@ private static void BlowChunks(byte[][]? chunks)
internal static PooledStream CreateWritableStream()
=> new ReadWriteStream();

public class PooledStream : Stream
public abstract class PooledStream : Stream
{
protected List<byte[]> chunks;

Expand All @@ -109,13 +106,7 @@ protected PooledStream(long length, List<byte[]> chunks)
this.chunks = chunks;
}

public override long Length
{
get
{
return this.length;
}
}
public override long Length => this.length;

public override bool CanRead => true;

Expand All @@ -130,10 +121,7 @@ public override void Flush()

public override long Position
{
get
{
return this.position;
}
get => this.position;

set
{
Expand Down Expand Up @@ -176,16 +164,9 @@ public override long Seek(long offset, SeekOrigin origin)
public override int ReadByte()
{
if (position >= length)
{
return -1;
}

var currentIndex = CurrentChunkIndex;
var chunk = chunks[currentIndex];

var currentOffset = CurrentChunkOffset;
var result = chunk[currentOffset];

var result = chunks[CurrentChunkIndex][CurrentChunkOffset];
this.position++;
return result;
}
Expand Down Expand Up @@ -295,7 +276,7 @@ public override void Write(byte[] buffer, int offset, int count)
{
}

private class ReadWriteStream : PooledStream
private sealed class ReadWriteStream : PooledStream
{
public ReadWriteStream()
: base(length: 0, chunks: SharedPools.BigDefault<List<byte[]>>().AllocateAndClear())
Expand Down
Loading