forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TraceAbleRequestContextStorage for ease of detect context leaks (l…
…ine#4232) Motivation: - Context leaks are hard to find because an exception does not tell where/which context is pushed without poping. By using TraceAbleRequestContextStorage, it helps to report the source of context leaks. - Details as mentioned in line#4100 By the way, Thanks to @anuraaga for giving a reference to read on [opentelemetry](https://github.com/open-telemetry/opentelemetry-java). Modifications: - Add `TraceAbleRequestContextStorage` that stores `RequestContext` stack trace and reports to the user where it happens. - Add `requestContextLeakDetectionSampler` flag that users can use for enable leak detection. Users can enable it by either system property or SPI flag provider. Result: - Closes line#4100 - `TraceAbleRequestContextStorage` is added, so users can use it to report where context leaks happen. How to enable: 1) By system property `-Dcom.linecorp.armeria.requestContextLeakDetectionSampler=<sampler-spec>` 2) By providing FlagsProvider SPI ```java public final class EnableLeakDetectionFlagsProvider implements FlagsProvider { @OverRide public Sampler<? super RequestContext> requestContextLeakDetectionSampler() { return Sampler.always(); } ... } ``` 3) By providing RequestContextStorageProvider SPI (not recommend since RequestContextStorageProvider SPI'll be remove as mentioned in #line#4211 ) ```java public final class CustomRequestContextStorageProvider implements RequestContextStorageProvider { @OverRide public RequestContextStorage newStorage() { return new TraceAbleRequestContextStorage(delegate); } } ``` Use case: Users problematic code ```java executor.execute(() -> { SafeCloseable leaked = fooCtx.push(); //This causes Request context leaks! ... }); executor.execute(() -> { try (SafeCloseable ignored = barCtx.push()) { //Exception happen here ... } }); ``` The above code will produce an error as below. Therefore, users can check the stack trace that which line causes context leaks. ``` java.lang.IllegalStateException: Trying to call object wrapped with context [%New RequestContext%], but context is currently set to TraceableServiceRequestContext[%Previous RequestContext%] com.linecorp.armeria.internal.common.LeakTracingRequestContextStorage$PendingRequestContextStackTrace: At thread [armeria-testing-eventloop-nio-1-1] previous RequestContext is pushed at the following stacktrace at com.linecorp.armeria.internal.common.LeakTracingRequestContextStorage$TraceableServiceRequestContext.<init>(LeakTracingRequestContextStorage.java:111) at com.linecorp.armeria.internal.common.LeakTracingRequestContextStorage$TraceableServiceRequestContext.<init>(LeakTracingRequestContextStorage.java:105) at com.linecorp.armeria.internal.common.LeakTracingRequestContextStorage.warpRequestContext(LeakTracingRequestContextStorage.java:82) at com.linecorp.armeria.internal.common.LeakTracingRequestContextStorage.push(LeakTracingRequestContextStorage.java:62) at com.linecorp.armeria.internal.common.RequestContextUtil.getAndSet(RequestContextUtil.java:149) at com.linecorp.armeria.server.ServiceRequestContext.push(ServiceRequestContext.java:221) at com.linecorp.armeria.internal.common.TraceRequestContextLeakTest.lambda$singleThreadContextLeak$2(TraceRequestContextLeakTest.java:101) <- This is the line where leaked RequestContext is push ... . This means the callback was called from unexpected thread or forgetting to close previous context. at com.linecorp.armeria.internal.common.RequestContextUtil.newIllegalContextPushingException(RequestContextUtil.java:100) at com.linecorp.armeria.server.ServiceRequestContext.push(ServiceRequestContext.java:237) at com.linecorp.armeria.internal.common.TraceRequestContextLeakTest.lambda$singleThreadContextLeak$3(TraceRequestContextLeakTest.java:107) ... ```
- Loading branch information
1 parent
aab6852
commit 55cb73b
Showing
21 changed files
with
775 additions
and
99 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
.../java/com/linecorp/armeria/internal/common/LeakTracingRequestContextStorageBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.internal.common; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
|
||
import com.linecorp.armeria.common.HttpMethod; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.RequestContextStorage; | ||
import com.linecorp.armeria.common.util.Sampler; | ||
import com.linecorp.armeria.server.ServiceRequestContext; | ||
|
||
/** | ||
* Microbenchmarks for LeakTracingRequestContextStorage. | ||
*/ | ||
public class LeakTracingRequestContextStorageBenchmark { | ||
|
||
private static final RequestContextStorage threadLocalReqCtxStorage = | ||
RequestContextStorage.threadLocal(); | ||
private static final RequestContextStorage neverSample = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.never()); | ||
private static final RequestContextStorage rateLimited1 = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.of("rate-limited=1")); | ||
private static final RequestContextStorage rateLimited10 = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.of("rate-limited=10")); | ||
private static final RequestContextStorage random1 = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.of("random=0.01")); | ||
private static final RequestContextStorage random10 = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.of("random=0.10")); | ||
private static final RequestContextStorage alwaysSample = | ||
new LeakTracingRequestContextStorage(threadLocalReqCtxStorage, Sampler.always()); | ||
private static final RequestContext reqCtx = newCtx("/"); | ||
|
||
private static ServiceRequestContext newCtx(String path) { | ||
return ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, path)) | ||
.build(); | ||
} | ||
|
||
@Benchmark | ||
public void baseline_threadLocal() { | ||
final RequestContext oldCtx = threadLocalReqCtxStorage.push(reqCtx); | ||
threadLocalReqCtxStorage.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_never_sample() { | ||
final RequestContext oldCtx = neverSample.push(reqCtx); | ||
neverSample.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_rateLimited_1() { | ||
final RequestContext oldCtx = rateLimited1.push(reqCtx); | ||
rateLimited1.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_rateLimited_10() { | ||
final RequestContext oldCtx = rateLimited10.push(reqCtx); | ||
rateLimited10.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_random_1() { | ||
final RequestContext oldCtx = random1.push(reqCtx); | ||
random1.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_random_10() { | ||
final RequestContext oldCtx = random10.push(reqCtx); | ||
random10.pop(reqCtx, oldCtx); | ||
} | ||
|
||
@Benchmark | ||
public void leakTracing_always_sample() { | ||
final RequestContext oldCtx = alwaysSample.push(reqCtx); | ||
alwaysSample.pop(reqCtx, oldCtx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.