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

Fix ActivityID map getting not cleaned up properly #1018

Closed
wants to merge 2 commits into from

Conversation

peterbae
Copy link
Contributor

@peterbae peterbae commented Apr 2, 2019

As mentioned in PR #465, the ActivityID objects in activityIdTlsMap would not get cleaned up in scenarios where there is a separate thread that manages the pooled connections. This is because the key that is used to look up and remove the entries in the map comes from the current thread's ID, and in a scenario where a separate thread is responsible for cleaning up all the threads, the thread's ID will stay constant throughout the rest of the application's lifetime and the ActivityIDs will not get cleaned up properly.

To solve this issue, instead of relying on recomputing the current thread's ID when we need to clean up the map, we associate each SQLServerConnection object with the thread ID, and use that variable later when we call close() on the connection. This will ensure that the map is getting cleaned up properly in all scenarios.

@codecov-io
Copy link

codecov-io commented Apr 2, 2019

Codecov Report

Merging #1018 into dev will decrease coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##                dev    #1018      +/-   ##
============================================
- Coverage     50.16%   50.13%   -0.04%     
+ Complexity     2885     2880       -5     
============================================
  Files           120      120              
  Lines         27989    27992       +3     
  Branches       4677     4676       -1     
============================================
- Hits          14040    14033       -7     
- Misses        11693    11700       +7     
- Partials       2256     2259       +3
Flag Coverage Δ Complexity Δ
#JDBC42 49.64% <100%> (-0.05%) 2839 <0> (-4)
#JDBC43 50.05% <100%> (ø) 2875 <0> (-4) ⬇️
Impacted Files Coverage Δ Complexity Δ
.../microsoft/sqlserver/jdbc/SQLServerConnection.java 46.63% <100%> (+0.03%) 344 <0> (ø) ⬇️
...m/microsoft/sqlserver/jdbc/ActivityCorrelator.java 94.44% <100%> (+0.15%) 7 <0> (ø) ⬇️
...om/microsoft/sqlserver/jdbc/ReaderInputStream.java 40.65% <0%> (-4.4%) 13% <0%> (-2%)
...ncurrentlinkedhashmap/ConcurrentLinkedHashMap.java 39% <0%> (-0.22%) 43% <0%> (-1%)
...om/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java 50.39% <0%> (-0.13%) 245% <0%> (-1%)
...rc/main/java/com/microsoft/sqlserver/jdbc/dtv.java 68.35% <0%> (-0.12%) 0% <0%> (ø)
...m/microsoft/sqlserver/jdbc/SQLServerResultSet.java 43.86% <0%> (-0.12%) 318% <0%> (-2%)
...in/java/com/microsoft/sqlserver/jdbc/IOBuffer.java 55.48% <0%> (ø) 0% <0%> (ø) ⬇️
...rc/main/java/com/microsoft/sqlserver/jdbc/DDC.java 47.3% <0%> (+0.43%) 112% <0%> (+1%) ⬆️

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 a64e479...fe7030c. Read the comment docs.

@peterbae
Copy link
Contributor Author

peterbae commented Apr 2, 2019

mssql-jdbc-7.3.1-SNAPSHOT.jre11-preview-PR1018.jar.zip
Uploading the jar for testing.

@@ -33,7 +31,7 @@ static ActivityId getCurrent() {

// Since the Id for each thread is unique, this assures that the below if statement is run only once per thread.
if (!activityIdTlsMap.containsKey(uniqueThreadId)) {
activityIdTlsMap.put(uniqueThreadId, new ActivityId());
activityIdTlsMap.put(uniqueThreadId, new ActivityId(uniqueThreadId));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Anywhere that calls getCurrent() is going to end up putting an entry in the map in the context of the current thread. In a pooled environment, each thread that uses the connection could potentially do that and then only the pooling thread that closes the connection will remove the original thread's entry from the map. There will still be a leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I'm looking into the documentation as to if Activity IDs are tied to a connection (one ActivityID per connection) or if it's tied to other queries as well. Depending on that, I think we can come up with a solution that doesn't affect the driver's performance - I'll come up with an update soon.

@peterbae
Copy link
Contributor Author

peterbae commented Apr 3, 2019

Continued in PR #1020.

@peterbae peterbae closed this Apr 3, 2019
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.

3 participants