-
Notifications
You must be signed in to change notification settings - Fork 18
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
Resilience4j integration #68
Open
sandboxo
wants to merge
7
commits into
master
Choose a base branch
from
resilience-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f1d0bb
resilience4j integration with resilience stream included
sandboxo 38bbd60
removed unnecessary try catch
sandboxo 463de0c
adding clojar repository
spawn92 17884f3
changing to https
spawn92 64b4ed1
changing corethreadpool size value to user defined concurrency value …
sandboxo 911d74a
master merge
sandboxo a791e43
Update DashboardModule.java
kingster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
15 changes: 15 additions & 0 deletions
15
core/src/main/java/com/flipkart/gjex/core/task/FutureProvider.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,15 @@ | ||
package com.flipkart.gjex.core.task; | ||
|
||
import io.reactivex.functions.BiConsumer; | ||
|
||
import java.util.concurrent.Future; | ||
|
||
public interface FutureProvider<T> extends HasExecutionProperties { | ||
|
||
Future<T> getFuture(); | ||
|
||
FutureProvider<T> clone(); | ||
|
||
void setCompletionConsumer(BiConsumer<T, Throwable> completionConsumer); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/com/flipkart/gjex/core/task/HasExecutionProperties.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,12 @@ | ||
package com.flipkart.gjex.core.task; | ||
|
||
public interface HasExecutionProperties { | ||
|
||
int getTimeout(); | ||
|
||
boolean isWithRequestHedging(); | ||
|
||
long getRollingTailLatency(); | ||
|
||
String getName(); | ||
} |
138 changes: 138 additions & 0 deletions
138
core/src/main/java/com/flipkart/gjex/core/task/ResilienceTaskExecutor.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,138 @@ | ||
package com.flipkart.gjex.core.task; | ||
|
||
import com.flipkart.gjex.core.logging.Logging; | ||
import io.github.resilience4j.bulkhead.ThreadPoolBulkhead; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreaker; | ||
import io.github.resilience4j.metrics.Timer; | ||
|
||
import io.grpc.Context; | ||
import io.reactivex.functions.BiConsumer; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.function.Supplier; | ||
|
||
public class ResilienceTaskExecutor<T> implements FutureProvider<T>, Logging { | ||
|
||
/** The MethodInvocation to execute asynchronously*/ | ||
private final MethodInvocation invocation; | ||
|
||
/** The currently active gRPC Context*/ | ||
private Context currentContext; | ||
|
||
/** The completion BiConsumer*/ | ||
private BiConsumer<T, Throwable> completionConsumer; | ||
|
||
/** Indicates if requests may be hedged within the configured timeout duration*/ | ||
private boolean withRequestHedging; | ||
|
||
/** The rolling tail latency as seen by Hystrix*/ | ||
private long rollingTailLatency; | ||
|
||
private int timeout; | ||
|
||
private final CircuitBreaker circuitBreaker; | ||
private final ThreadPoolBulkhead threadPoolBulkhead; | ||
private final Timer timer; | ||
|
||
public ResilienceTaskExecutor(MethodInvocation invocation, | ||
CircuitBreaker circuitBreaker, | ||
ThreadPoolBulkhead bulkhead, | ||
Timer timer, | ||
Boolean withRequestHedging, | ||
int timeout | ||
) { | ||
currentContext = Context.current(); | ||
this.invocation = invocation; | ||
this.circuitBreaker = circuitBreaker; | ||
this.timer = timer; | ||
this.threadPoolBulkhead = bulkhead; | ||
this.withRequestHedging = withRequestHedging; | ||
this.timeout = timeout; | ||
} | ||
|
||
public ResilienceTaskExecutor<T> clone() { | ||
ResilienceTaskExecutor<T> clone = | ||
new ResilienceTaskExecutor<T>( | ||
this.invocation, | ||
this.circuitBreaker, | ||
this.threadPoolBulkhead, | ||
this.timer, | ||
this.withRequestHedging, | ||
this.timeout | ||
); | ||
return clone; | ||
} | ||
|
||
public void setCompletionConsumer(BiConsumer<T, Throwable> completionConsumer) { | ||
this.completionConsumer = completionConsumer; | ||
} | ||
|
||
public MethodInvocation getInvocation() { | ||
return invocation; | ||
} | ||
|
||
public int getTimeout() { | ||
return timeout; | ||
} | ||
|
||
public boolean isWithRequestHedging() { | ||
return withRequestHedging; | ||
} | ||
|
||
public long getRollingTailLatency() { | ||
//TODO: hardcoded value kept here for now. | ||
// Need to figure out a way to get rolling 95th percentile latency in resilience | ||
return 10000L; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return getInvocation().getMethod().getName(); | ||
} | ||
|
||
public CompletableFuture<T> getFuture() { | ||
Supplier<T> supplier = prepareBaseSupplier(); | ||
return decorateSupplierWithResilience(supplier) | ||
.get() | ||
.toCompletableFuture(); | ||
} | ||
|
||
private Supplier<T> prepareBaseSupplier() { | ||
return () -> { | ||
try { | ||
return run(); | ||
} catch (Exception e) { | ||
error("Error executing task", e); | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
} | ||
|
||
public T run() throws Exception { | ||
Context previous = this.currentContext.attach(); // setting the current gRPC context for the executing Hystrix thread | ||
Throwable error = null; | ||
T result = null; | ||
try { | ||
result = ((AsyncResult<T>)this.invocation.proceed()).invoke(); // call the AsyncResult#invoke() to execute the actual work to be performed asynchronously | ||
return result; | ||
} catch (Throwable e) { | ||
error = e; | ||
error("Error executing task", e); | ||
throw new RuntimeException(e); | ||
} finally { | ||
if (this.completionConsumer != null) { | ||
this.completionConsumer.accept(result, error); // inform the completion status to the registered completion consumer | ||
} | ||
this.currentContext.detach(previous); // unset the current gRPC context | ||
} | ||
} | ||
|
||
public <T> Supplier<CompletionStage<T>> decorateSupplierWithResilience(Supplier<T> baseSupplier) { | ||
Supplier<T> cbSupplier = circuitBreaker.decorateSupplier(baseSupplier); | ||
Supplier<CompletionStage<T>> tpbSupplier = threadPoolBulkhead.decorateSupplier(cbSupplier); | ||
return Timer.decorateCompletionStageSupplier(timer, tpbSupplier); | ||
} | ||
|
||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we instead use the latest version of metrics?
libraries.dw_metrics
is already included.