Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Native Event Tracing IO Wrappers #1444

Merged
merged 21 commits into from
Aug 6, 2020
Merged

Conversation

seanjparker
Copy link
Contributor

@seanjparker seanjparker commented Jul 30, 2020

For UTY-2685

Description

  • Added native C# wrappers for IO operations with the Worker SDK

Tests

How did you test these changes prior to submitting this pull request?
What automated tests are included in this PR?

  • Manually tested that native event wrappers worked with the current C-C# bindings

Documentation

How is this documented (for example: release note, upgrade guide, feature page, in-code documentation)?

  • Changelog

@improbable-prow-robot improbable-prow-robot added jira/no-ticket Indicates a PR has no corresponding JIRA ticket do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jul 30, 2020
@improbable-prow-robot improbable-prow-robot added the size/L Denotes a PR that changes 150-299 lines, ignoring generated files. label Jul 30, 2020
@seanjparker seanjparker requested a review from BryanJY-Wong July 30, 2020 16:20
@seanjparker seanjparker marked this pull request as ready for review July 30, 2020 16:20
@improbable-prow-robot improbable-prow-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 30, 2020
Sean Parker added 2 commits August 3, 2020 14:25
@seanjparker seanjparker requested a review from zeroZshadow August 3, 2020 14:06
{
fixed (byte* fileNameBytes = ApiInterop.ToUtf8Cstr(fileName))
{
return new IOStream(CIO.CreateFileStream(fileNameBytes, CIO.OpenMode.OpenModeDefault));
return new IOStream(CIO.CreateFileStream(fileNameBytes, (CIO.OpenMode) openMode));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this kind of cast is actually valid. the C# Interop has some fancy conversion class for this reason.
Might want to double check

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be a compiler error if it was an invalid cast I think (the as casts fail at runtime)

Copy link
Contributor

@jamiebrynes7 jamiebrynes7 left a comment

Choose a reason for hiding this comment

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

As a general comment, we should probably throw ObjectDisposedException on stream operations if the object has already been disposed

{
fixed (byte* fileNameBytes = ApiInterop.ToUtf8Cstr(fileName))
{
return new IOStream(CIO.CreateFileStream(fileNameBytes, CIO.OpenMode.OpenModeDefault));
return new IOStream(CIO.CreateFileStream(fileNameBytes, (CIO.OpenMode) openMode));
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be a compiler error if it was an invalid cast I think (the as casts fail at runtime)

var remainingCapacity = CIO.StreamGetRemainingWriteCapacityBytes(stream);
if (remainingCapacity < data.Length)
{
throw new IOException("Not enough stream capacity to write data.");
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worth have a different exception for this case as opposed to the general error case. It means users can write logic to specifically handle it.

It seems that NotSupportedException may be a good candidate as its used elsewhere in the C# standard library for 'stream full' errors (e.g. - in Memory Stream)

Comment on lines 101 to 118
public byte[] Peek(uint bytes)
{
var streamData = new byte[bytes];

var bytesPeeked = 0L;
fixed (byte* streamDataPointer = streamData)
{
bytesPeeked = CIO.StreamPeek(stream, streamDataPointer, bytes);
}

if (bytesPeeked != -1)
{
return streamData;
}

var rawError = CIO.StreamGetLastError(stream);
throw new IOException(ApiInterop.FromUtf8Cstr(rawError));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this public uint Peek(uint bytes, out byte[] bytes) to match the Read method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Read method has two implementations, one where the caller provides the bytes array which is written to, and the other takes a the number of bytes to read and creates the array for them.

Should I add another implementation of Peek so that it matches Read?

throw new IOException(ApiInterop.FromUtf8Cstr(rawError));
}

public void Ignore(uint bytesToIgnore)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably return bytesIgnored

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, good point

Copy link
Contributor

@jamiebrynes7 jamiebrynes7 left a comment

Choose a reason for hiding this comment

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

LGTM % naming nit

Nice work!

return CIO.StreamGetRemainingWriteCapacityBytes(stream);
}

private void CheckIfStreamClosed()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Prefer ThrowIfStreamClosed over CheckIfStreamClosed, I would assume CheckIfStreamClosed would return me a result rather than throwing an exception.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 4, 2020

Please retry analysis of this Pull-Request directly on SonarCloud.

@seanjparker seanjparker merged commit ad77599 into develop Aug 6, 2020
@improbable-prow-robot improbable-prow-robot deleted the feature/native-event-tracing branch August 6, 2020 16:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
jira/no-ticket Indicates a PR has no corresponding JIRA ticket size/L Denotes a PR that changes 150-299 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants