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 RequestScopedMdc properties are inherited #2729

Merged
merged 9 commits into from May 20, 2020

Conversation

trustin
Copy link
Member

@trustin trustin commented May 19, 2020

Motivation:

A user will expect the following case works:

ServiceRequestContext sctx = ...;
try (SafeCloseable ignored = sctx.push()) {
    RequestScopedMdc.put(sctx, "transactionId", "1234");

    ClientRequestContext cctx = ...;
    try (SafeCloseable ignored2 = cctx.push()) {
        assert MDC.get("transactionId").equals("1234");
    }
}

.. which does not work currently, because RequestScopedMdc does not
look up the root context's map.

Modifications:

  • Make RequestScopedMdc.get(), getAll() and getCopyOfContextMap()
    look for the root context map.
  • Reduced the memory footprint of overall map iteration by using
    only Object2ObjectMaps.
    • Object2ObjectMap uses a special variant of Iterator when iterating
      over other Object2ObjectMaps.
  • Reduced the memory footprint of copyAll() and getCopyOfContextMap()
    by using getPropertyMap().

Result:

  • RequestScopedMdc properties are inherited from the root context.

Motivation:

A user will expect the following case works:

    ServiceRequestContext sctx = ...;
    try (SafeCloseable ignored = sctx.push()) {
        RequestScopedMdc.put(sctx, "transactionId", "1234");

        ClientRequestContext cctx = ...;
        try (SafeCloseable ignored2 = cctx.push()) {
            assert MDC.get("transactionId").equals("1234");
        }
    }

.. which does not work currently, because `RequestScopedMdc` does not
look up the root context's map.

Modifications:

- Make `RequestScopedMdc.get()`, `getAll()` and `getCopyOfContextMap()`
  look for the root context map.

Result:

- `RequestScopedMdc` properties are inherited from the root context.
@trustin trustin added this to the 0.99.6 milestone May 19, 2020
@codecov
Copy link

codecov bot commented May 19, 2020

Codecov Report

Merging #2729 into master will decrease coverage by 0.06%.
The diff coverage is 83.33%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2729      +/-   ##
============================================
- Coverage     72.68%   72.62%   -0.07%     
+ Complexity    11638    11631       -7     
============================================
  Files          1025     1025              
  Lines         45568    45605      +37     
  Branches       5674     5683       +9     
============================================
- Hits          33120    33119       -1     
- Misses         9541     9566      +25     
- Partials       2907     2920      +13     
Impacted Files Coverage Δ Complexity Δ
...ecorp/armeria/common/logging/RequestScopedMdc.java 87.73% <83.33%> (-6.72%) 23.00 <3.00> (+5.00) ⬇️
...inecorp/armeria/common/ClosedSessionException.java 36.36% <0.00%> (-36.37%) 4.00% <0.00%> (-2.00%)
...necorp/armeria/internal/common/brave/SpanTags.java 66.66% <0.00%> (-33.34%) 3.00% <0.00%> (-2.00%)
...com/linecorp/armeria/client/brave/BraveClient.java 80.32% <0.00%> (-9.84%) 12.00% <0.00%> (-3.00%)
...rmeria/internal/common/brave/TraceContextUtil.java 95.83% <0.00%> (-4.17%) 5.00% <0.00%> (ø%)
.../armeria/client/endpoint/dns/DnsEndpointGroup.java 78.12% <0.00%> (-3.13%) 13.00% <0.00%> (-2.00%)
...ria/server/brave/ServiceRequestContextAdapter.java 81.81% <0.00%> (-3.04%) 4.00% <0.00%> (ø%)
...om/linecorp/armeria/client/HttpSessionHandler.java 62.25% <0.00%> (-2.65%) 40.00% <0.00%> (-2.00%)
...rmeria/common/logging/ClientConnectionTimings.java 70.58% <0.00%> (-1.97%) 9.00% <0.00%> (-1.00%)
...corp/armeria/common/NonWrappingRequestContext.java 79.24% <0.00%> (-1.89%) 21.00% <0.00%> (-1.00%)
... and 16 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 5dcdecd...92a2cad. Read the comment docs.

}
ctx.setAttr(MAP, Collections.unmodifiableMap(newMap));
} catch (Throwable t) {
Exceptions.throwUnsafely(t);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it better to log (maybe once?) and return delegate.getCopyOfContextMap(); in this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

We will never reach here, because we made sure invokeExact() works in the class initializer. If any exception is raised, it means it's a bug. Let me add some comment about this.

final Object2ObjectOpenHashMap<String, String> merged;
if (requestScopedMap instanceof Object2ObjectOpenHashMap) {
// Reuse the mutable copy returned by getAll() for less memory footprint.
merged = (Object2ObjectOpenHashMap<String, String>) requestScopedMap;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this unmodifiable if the map is returned from here? https://github.com/line/armeria/pull/2729/files#diff-b35989b7cd6a82522044209c418a377dR193

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. The map will not be an Object2ObjectOpenHashMap but be an Object2ObjectMaps.Unmodifiable or something. This code path is for the case where getAll() returns a mutable map, so we do not create two maps when merging twice.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I thought the condition was requestScopedMap instanceof Object2ObjectMap. Sorry about it. 😉

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Thanks! @trustin

@trustin trustin merged commit f3b96e0 into line:master May 20, 2020
@trustin trustin deleted the own_attrs_2 branch May 20, 2020 04:19
@trustin
Copy link
Member Author

trustin commented May 20, 2020

Thanks for reviewing.

fmguerreiro pushed a commit to fmguerreiro/armeria that referenced this pull request Sep 19, 2020
Motivation:

A user will expect the following case works:

    ServiceRequestContext sctx = ...;
    try (SafeCloseable ignored = sctx.push()) {
        RequestScopedMdc.put(sctx, "transactionId", "1234");

        ClientRequestContext cctx = ...;
        try (SafeCloseable ignored2 = cctx.push()) {
            assert MDC.get("transactionId").equals("1234");
        }
    }

.. which does not work currently, because `RequestScopedMdc` does not
look up the root context's map.

Modifications:

- Make `RequestScopedMdc.get()`, `getAll()` and `getCopyOfContextMap()`
  look for the root context map.
- Reduced the memory footprint of overall map iteration by using
  only `Object2ObjectMap`s.
  - `Object2ObjectMap` uses a special variant of `Iterator` when iterating
    over other `Object2ObjectMap`s.
- Reduced the memory footprint of `copyAll()` and `getCopyOfContextMap()`
  by using `getPropertyMap()`.

Result:

- `RequestScopedMdc` properties are inherited from the root context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants