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

Make DateSerializer thread-safe #2430

Merged
merged 1 commit into from
Feb 14, 2021

Conversation

cdegroc
Copy link
Contributor

@cdegroc cdegroc commented Feb 3, 2021

Fixes #2429

Signed-off-by: Clement de Groc [email protected]


Thank you for contributing to JanusGraph!

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there an issue associated with this PR? Is it referenced in the commit message?
  • Does your PR body contain #xyz where xyz is the issue number you are trying to resolve?
  • Has your PR been rebased against the latest commit within the target branch (typically master)?
  • Is your initial contribution a single, squashed commit?

For code changes:

  • Have you written and/or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE.txt file, including the main LICENSE.txt file in the root of this repository?
  • If applicable, have you updated the NOTICE.txt file, including the main NOTICE.txt file found in the root of this repository?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

@codecov-io
Copy link

codecov-io commented Feb 3, 2021

Codecov Report

Merging #2430 (aef1d62) into master (9aff938) will decrease coverage by 11.07%.
The diff coverage is 50.00%.

Impacted file tree graph

@@              Coverage Diff              @@
##             master    #2430       +/-   ##
=============================================
- Coverage     70.37%   59.30%   -11.08%     
+ Complexity     7764     5484     -2280     
=============================================
  Files           687      566      -121     
  Lines         30047    24444     -5603     
  Branches       4830     4116      -714     
=============================================
- Hits          21145    14496     -6649     
- Misses         6385     7877     +1492     
+ Partials       2517     2071      -446     
Impacted Files Coverage Δ Complexity Δ
...b/database/serialize/attribute/DateSerializer.java 35.29% <50.00%> (-41.18%) 3.00 <1.00> (-4.00)
.../src/main/java/org/janusgraph/core/log/Change.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...anusgraph/diskstorage/util/CacheMetricsAction.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...usgraph/diskstorage/TemporaryBackendException.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
...diskstorage/locking/TemporaryLockingException.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-3.00%)
.../database/cache/MetricInstrumentedSchemaCache.java 0.00% <0.00%> (-100.00%) 0.00% <0.00%> (-5.00%)
...onfiguration/validator/CompatibilityValidator.java 0.00% <0.00%> (-87.50%) 0.00% <0.00%> (-5.00%)
...kerpop/optimize/step/JanusGraphMultiQueryStep.java 0.00% <0.00%> (-87.50%) 0.00% <0.00%> (-7.00%)
...anusgraph/diskstorage/util/DefaultTransaction.java 0.00% <0.00%> (-85.72%) 0.00% <0.00%> (-3.00%)
...king/consistentkey/StandardLockCleanerService.java 0.00% <0.00%> (-84.38%) 0.00% <0.00%> (-4.00%)
... and 331 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 9aff938...aef1d62. Read the comment docs.

@janusgraph-bot janusgraph-bot added the cla: external Externally-managed CLA label Feb 3, 2021
@li-boxuan
Copy link
Member

@farodin91 Seems multiple PRs are failing due to

Error: JanusGraphScriptIT.testGraphOfTheGodsGraphSONFull:26->JanusGraphAssemblyBaseIT.unzipAndRunExpect:102->JanusGraphAssemblyBaseIT.unzipAndRunExpect:73->JanusGraphAssemblyBaseIT.parseTemplateAndRunExpect:95->JanusGraphAssemblyBaseIT.expect:113->JanusGraphAssemblyBaseIT.command:152 expected: <0> but was: <1>

Would you be able to take a look at it?

@farodin91
Copy link
Contributor

@farodin91 Seems multiple PRs are failing due to

Error: JanusGraphScriptIT.testGraphOfTheGodsGraphSONFull:26->JanusGraphAssemblyBaseIT.unzipAndRunExpect:102->JanusGraphAssemblyBaseIT.unzipAndRunExpect:73->JanusGraphAssemblyBaseIT.parseTemplateAndRunExpect:95->JanusGraphAssemblyBaseIT.expect:113->JanusGraphAssemblyBaseIT.command:152 expected: <0> but was: <1>

Would you be able to take a look at it?

I will provide an pull request. If you want to fix it quick check for the newest Cassandra version and replace the version in janusgraph dist.

@cdegroc
Copy link
Contributor Author

cdegroc commented Feb 5, 2021

Rebased on master to have the new Cassandra version.

Copy link
Member

@li-boxuan li-boxuan left a comment

Choose a reason for hiding this comment

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

LGTM 👍
From this discussion it seems we shouldn't have used this StdDateFormat in the first place, but this fix/workaround looks good to me.

@cdegroc
Copy link
Contributor Author

cdegroc commented Feb 9, 2021

LGTM 👍
From this discussion it seems we shouldn't have used this StdDateFormat in the first place, but this fix/workaround looks good to me.

Agree. It would be cleaner to switch to Java 8+ DateTimeFormatter but, to remain backward compatible, we'd have to try both ISO-8601 and RFC-1123 date/time patterns. Or just discard the RFC-1123 pattern.

@li-boxuan li-boxuan added this to the Release v0.6.0 milestone Feb 14, 2021
@li-boxuan li-boxuan merged commit fb8bcbc into JanusGraph:master Feb 14, 2021
@li-boxuan
Copy link
Member

Merged by following lazy consensus.

@@ -26,7 +26,8 @@
public class DateSerializer implements OrderPreservingSerializer<Date> {

private final LongSerializer ls = LongSerializer.INSTANCE;
private final StdDateFormat dateFormat = StdDateFormat.instance;
// StdDateFormat is not thread-safe
private static final ThreadLocal<StdDateFormat> dateFormat = ThreadLocal.withInitial(StdDateFormat::new);
Copy link
Member

Choose a reason for hiding this comment

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

Didn't check the logic but wanted to say that ThreadLocal data is not released if the thread is killed / stopped.
Which means that we can see data leak here if we don't properly remove threadlocal data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @porunov. Good point. Glad you mentioned it. I'll open another PR to fix this.

Copy link
Member

Choose a reason for hiding this comment

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

Hi is there any resource that explains how/why ThreadLocal data is not garbage collected for a dead thread?

Copy link
Member

Choose a reason for hiding this comment

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

Quickly reading javadoc I'm now in doubt about my words. Will need to check more info later. Maybe I'm just wrong. I do remember memory leaks with ThreadLocal but maybe it's only reproducible with cached thread pool. I will check that later

Copy link
Contributor Author

@cdegroc cdegroc Feb 17, 2021

Choose a reason for hiding this comment

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

Here is a reference (from Sonar) to leaks happening when re-using threads: https://rules.sonarsource.com/java/RSPEC-5164
If you believe there is a leak, and as already mentioned on this PR/issue, we can always switch to using the thread-safe Java 8+ DateTimeFormatter.

I've created a branch with a candidate change set: master...cdegroc:java-date-time-formatter

Copy link
Member

Choose a reason for hiding this comment

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

I do remember memory leaks with ThreadLocal

I think you are referring to the case where the transaction (or even janusgraph) is closed but the thread (likely part of an externally managed thread pool) is reused for other affairs. E.g. #1341

Given this StdDateFormat shouldn't be used in the first place since it is intended for internal use only, I agree we should switch to alternative solutions. That being said, I haven't reviewed @cdegroc 's thread-safe solution yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #2547 as a follow-up.

@cdegroc cdegroc deleted the thread-safe-date-serializer branch February 15, 2021 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: external Externally-managed CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DateSerializer is not thread-safe
6 participants