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

Changed logging logic in AE and improved performance #773

Merged
merged 4 commits into from
Aug 17, 2018

Conversation

rene-ye
Copy link
Member

@rene-ye rene-ye commented Aug 3, 2018

Fixed an issue where the logger was needlessly traversing the stack to obtain method name.

try {
// AES encryption CBC mode and PKCS5 padding
decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec, ivector);
if (decryptCipher != null) {
Copy link

@saurabh500 saurabh500 Aug 3, 2018

Choose a reason for hiding this comment

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

Why not declare, initialize and instantiate the decryptCipher here itself? Why is a static member to this class needed when the Cipher.getInstance(...) itself is static?

Choose a reason for hiding this comment

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

Looks like the code earlier was doing what I am talking about, above. Why is this change for declaring decryptCipher as a static class member needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no reason for the decryptCipher variable to be static, I will change this. However, I believe it should be done once in the constructor as there isn't a point to get an instance for every decryptData(byte,byte,int,int) call.

Choose a reason for hiding this comment

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

The issue I see with having this in the constructor is that you are replicating the behavior of Cipher.getInstance() which is static itself and adding this additional layer of caching and making assumption about the API that it would be safe to use the same object when the API itself doesn't make any such guarantees in the documentation.
A static is good if you want to cache the object at your end and it is safe to do so, but since Cipher.getInstance(...) is static itself, it should decide whether it should give a new object everytime or return a cached object.

In case the behavior of Cipher.getInstance() changes for any reason across JDK versions, then this driver will have to comb through this caching. Such bugs are hard to detect. Such layers of caching of objects returned from static call is not a good practice.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, I will revert the changes.

@saurabh500
Copy link

What is the cause of degradation? The logger traversing the stack or lack of initialization of cipher?

@codecov-io
Copy link

codecov-io commented Aug 3, 2018

Codecov Report

Merging #773 into dev will decrease coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##                dev     #773      +/-   ##
============================================
- Coverage     48.17%   48.14%   -0.03%     
- Complexity     2776     2777       +1     
============================================
  Files           116      116              
  Lines         27854    27848       -6     
  Branches       4636     4636              
============================================
- Hits          13418    13408      -10     
- Misses        12215    12219       +4     
  Partials       2221     2221
Flag Coverage Δ Complexity Δ
#JDBC42 47.6% <100%> (-0.11%) 2733 <4> (-2)
#JDBC43 48.06% <100%> (+0.04%) 2770 <4> (+1) ⬆️
Impacted Files Coverage Δ Complexity Δ
...SQLServerColumnEncryptionJavaKeyStoreProvider.java 63.12% <ø> (-0.52%) 10 <0> (ø)
...r/jdbc/SQLServerAeadAes256CbcHmac256Algorithm.java 70.54% <100%> (-0.89%) 11 <4> (ø)
...om/microsoft/sqlserver/jdbc/ReaderInputStream.java 41.75% <0%> (-3.3%) 14% <0%> (-2%)
...om/microsoft/sqlserver/jdbc/SimpleInputStream.java 51.47% <0%> (-1.48%) 11% <0%> (-1%)
...ncurrentlinkedhashmap/ConcurrentLinkedHashMap.java 38.14% <0%> (-0.87%) 42% <0%> (-1%)
...n/java/com/microsoft/sqlserver/jdbc/Parameter.java 62.92% <0%> (-0.81%) 64% <0%> (ø)
...oft/sqlserver/jdbc/SQLServerPreparedStatement.java 52.04% <0%> (-0.2%) 217% <0%> (ø)
...rc/main/java/com/microsoft/sqlserver/jdbc/dtv.java 62.49% <0%> (-0.06%) 0% <0%> (ø)
...in/java/com/microsoft/sqlserver/jdbc/IOBuffer.java 53.86% <0%> (+0.06%) 0% <0%> (ø) ⬇️
...om/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java 52.72% <0%> (+0.11%) 263% <0%> (+1%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e301555...ca11ab9. Read the comment docs.

try {
decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// not possible to get here
Copy link

Choose a reason for hiding this comment

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

From the comment, it sounds like these exceptions is not expected to ever happen. How come we are try-catching and then swallowing these exceptions then? Shouldn't we let these exceptions get thrown if something unexpected occurs?

Choose a reason for hiding this comment

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

These exceptions must be logged. I can see the source of OpenJDK where this exception can be thrown. In case the Cipher is not found in the security providers, then an exception can definitely surface.

@rene-ye
Copy link
Member Author

rene-ye commented Aug 7, 2018

Uploading jars. These jars contain the final changes that will be going into the driver.
AE Jars.zip

@cheenamalhotra cheenamalhotra added this to the 7.1.0 milestone Aug 16, 2018
@rene-ye rene-ye merged commit ddc1d94 into microsoft:dev Aug 17, 2018
@rene-ye rene-ye deleted the dev-resiliancy branch August 17, 2018 21:00
@rene-ye rene-ye changed the title Degradation fix for column encryption enabled (AE) Removed logging logic which caused performance degradation in AE Aug 28, 2018
@rene-ye rene-ye changed the title Removed logging logic which caused performance degradation in AE Changed logging logic in AE and improved performance Aug 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants