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

Support For Big Endian Systems #2170

Merged
merged 3 commits into from Dec 8, 2023
Merged

Conversation

saitama951
Copy link
Contributor

The Microsoft SqlClient driver crashes on the s390x. This error stems mostly from the use of BitConverter function used in method definitions across the code and improper byte ordering for big endian systems.

The BitConverter class does conversions according to the endianess of the system it is operating on which causes incorrect reads of little-endian packets sent by the server.

@saitama951 saitama951 force-pushed the bigendianfix branch 3 times, most recently from 35fb50f to ab3e871 Compare September 26, 2023 19:28
@codecov
Copy link

codecov bot commented Sep 27, 2023

Codecov Report

Attention: 21 lines in your changes are missing coverage. Please review.

Comparison is base (4319661) 72.48% compared to head (0333a90) 71.30%.
Report is 1 commits behind head on main.

❗ Current head 0333a90 differs from pull request most recent head 499bae3. Consider uploading reports for the commit 499bae3 to get more accurate results

Files Patch % Lines
.../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs 22.22% 14 Missing ⚠️
...icrosoft/Data/SqlClient/SqlSequentialTextReader.cs 20.00% 4 Missing ⚠️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 33.33% 2 Missing ⚠️
...oft/Data/SqlClient/TdsParserStateObject.netcore.cs 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2170      +/-   ##
==========================================
- Coverage   72.48%   71.30%   -1.18%     
==========================================
  Files         309      306       -3     
  Lines       61959    61892      -67     
==========================================
- Hits        44911    44135     -776     
- Misses      17048    17757     +709     
Flag Coverage Δ
addons 92.88% <ø> (ø)
netcore 73.73% <67.18%> (-2.94%) ⬇️
netfx 70.16% <25.00%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@saitama951
Copy link
Contributor Author

Hi, @Wraith2 @David-Engel can you please review this?
Thanks

Comment on lines 1764 to 1769
var bytes = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(bytes, BitConverter.SingleToInt32Bits(v));
return bytes;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to do this using the span we already have instead of allocating a new byte[]? If an array is needed it could be stackalloc-ed since we know it is fixed size.

Copy link
Member

Choose a reason for hiding this comment

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

@saitama951 any answer to this query?

@saitama951 saitama951 force-pushed the bigendianfix branch 3 times, most recently from db4fb62 to 4114d8a Compare September 28, 2023 11:17
@saitama951
Copy link
Contributor Author

saitama951 commented Sep 28, 2023

@Wraith2 Thank you for the suggestions, I have made the changes. not sure why the CI is failing, specifically win 2022 one's (Failed to start Sql browser)

@JRahnama
Copy link
Member

@saitama951 failures are not related to your changes. Somehow starting SqlBrowser task fails on the pipelines. We will look into them.

@Wraith2
Copy link
Contributor

Wraith2 commented Sep 29, 2023

I'm a little concerned about the number of preprocessor regions. If there's any way to extract common patterns into functions so we have fewer ifdefs it'd be nice. Otherwise without the ability to test i'll go with your changes as long as you've got the tests working.

Out of interest are you using managed networking? or is there a big endian native sni.dll ?

@saitama951 saitama951 force-pushed the bigendianfix branch 11 times, most recently from 94fbadc to 08d42b1 Compare October 2, 2023 16:52
@saitama951
Copy link
Contributor Author

I'm a little concerned about the number of preprocessor regions. If there's any way to extract common patterns into functions so we have fewer ifdefs it'd be nice. Otherwise without the ability to test i'll go with your changes as long as you've got the tests working.

Out of interest are you using managed networking? or is there a big endian native sni.dll ?

I agree with you the if defs are unnecessarily populating the code, I do see the csproj includes a package System.Memory which supports BinaryPrimitives for all the targets but some functions are not supported with netstandard2.0 so I have added a file with the compatibility support, let me know your thoughts on this.
to answer your second question it uses the managed networking dll.

@DavoudEshtehari
Copy link
Member

My concern is how this change is going to impact the performance by impacting the hot paths. Does anybody have a perf test to compare it with before the change?

@saitama951
Copy link
Contributor Author

@DavoudEshtehari
I'm looking into the perf tests.

@saitama951
Copy link
Contributor Author

saitama951 commented Nov 22, 2023

@DavoudEshtehari
Hi,
I did run the performance tests and I'am seeing some noise and inconsistency in the results with the patch and without the patch for all the various number of times I run the tests. any guidance on this would help.
Thanks

@Wraith2
Copy link
Contributor

Wraith2 commented Nov 22, 2023

They're quite noisy. Unless the differences are very large I wouldn't worry about it too much.

@DavoudEshtehari
Copy link
Member

@saitama951 Make sure there is no heavy background process running while your test is running.


namespace Microsoft.Data.SqlClient
{
internal static class BitConverterCompat
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we truncate a word here/spelling mistake? Are you trying to say Compatible/Compact? I think it's okay to make it more clear if you need to say Compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@H-Yeo Yes, This was meant to be Compatible (since certain functions of bitconverter were not supported in older frameworks). I will make the necessary change .Thanks

@DavoudEshtehari
Copy link
Member

DavoudEshtehari commented Dec 5, 2023

@saitama951 May I ask you to share the perf project and the result from your perf test run?

@saitama951
Copy link
Contributor Author

saitama951 commented Dec 6, 2023

@DavoudEshtehari Sorry for the delay in response, I'am running the performance tests present in the SqlClient and was using the ResultComparer tool to compare the results with and without the patch. Initially I was facing a lot of issue in generating a consistent set of data with various runs. As of now I'am able to pertain the consistency and see no reproducible regression, here are the perf results from the various runs.
run-1.xlsx
run-2.xlsx
run-3.xlsx
run-4.xlsx

@saitama951
Copy link
Contributor Author

In Addition to the files, here is a summary output

[omen ResultsComparer]# dotnet run -c Release --base /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/without_change/result-1/results/ --diff /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/with_change/result-1/results/ --threshold 10% --full-id -f net7.0
summary:
better: 1, geomean: 1.420
total diff: 1

No Slower results for the provided threshold = 10% and noise filter = 0.3 ns.

| Faster                                                                   | base/diff | Base Median (ns) | Diff Median (ns) | Modality|
| ------------------------------------------------------------------------ | ---------:| ----------------:| ----------------:| --------:|
| Microsoft.Data.SqlClient.PerformanceTests.SqlCommandRunner.ExecuteScalar |      1.42 |      49766159.25 |      35054435.75 |         |


[omen ResultsComparer]# dotnet run -c Release --base /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/without_change/result-2/results/ --diff /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/with_change/result-2/results/ --threshold 10% --full-id -f net7.0
**No differences found between the benchmark results with threshold 10%.**

[omen ResultsComparer]# dotnet run -c Release --base /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/without_change/result-3/results/ --diff /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/with_change/result-3/results/ --threshold 10% --full-id -f net7.0
**No differences found between the benchmark results with threshold 10%.**

[omen ResultsComparer]# dotnet run -c Release --base /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/without_change/result-4/results/ --diff /home/manjaro/sqlclient-patch/SqlClient/src/Microsoft.Data.SqlClient/tests/PerformanceTests/with_change/result-4/results/ --threshold 10% --full-id -f net7.0
**No differences found between the benchmark results with threshold 10%.**

[omen ResultsComparer]#

Specs:

BenchmarkDotNet=v0.13.2, OS=manjaro 
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.111
  [Host] : .NET 7.0.11 (7.0.1123.46301), X64 RyuJIT AVX2

SqlClient v5.2 automation moved this from In progress to Reviewer approved Dec 7, 2023
@JRahnama
Copy link
Member

JRahnama commented Dec 7, 2023

@saitama951 can you address the conflict so we can proceed with the preview release today?

Copy link
Contributor

@panoskj panoskj left a comment

Choose a reason for hiding this comment

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

You have reverted some changes I introduced with #2168 . Note this file is shared between netcore and netfx versions. I highlighted two examples, but there are more lines.

@panoskj
Copy link
Contributor

panoskj commented Dec 7, 2023

@saitama951 The last 3 commits look good now. I haven't reviewed the merge commit at all, it will take me some time. By the way I would squash these commits since reviewing them individually is harder.

@saitama951 saitama951 force-pushed the bigendianfix branch 7 times, most recently from 27ad06e to 19d7865 Compare December 7, 2023 22:03
revert some changes

Update TdsParserStateObject.cs

change bitconvertercompat to bitconvertercompatible
@JRahnama JRahnama merged commit d18e5c1 into dotnet:main Dec 8, 2023
117 of 147 checks passed
SqlClient v5.2 automation moved this from Reviewer approved to Done Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💡 Enhancement New feature request
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

7 participants