-
Notifications
You must be signed in to change notification settings - Fork 208
Implementing Retry logic [Reliable Channel] #556
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
Changes from all commits
425e00f
641ef58
dc2fff7
efac9ce
9431ded
c2826ae
094383a
eb8eeb8
cd32481
d8edde3
6699afb
1e5d72c
3a36811
f917a83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.microsoft.applicationinsights.internal.channel; | ||
|
|
||
| /** | ||
| * An interface that is used to create a concrete class that is called by the the {@link TransmissionHandlerObserver} | ||
| * <p> | ||
| * This is used to implement classes like {@link ErrorHandler} and {@link PartialSuccessHandler}. | ||
| * @author jamdavi | ||
| * | ||
| * | ||
| */ | ||
| public interface TransmissionHandler { | ||
| /** | ||
| * Called when a transmission is sent by the {@link TransmissionOutput}. | ||
| * @param args The {@link TransmissionHandlerArgs} for this handler. | ||
| */ | ||
| void onTransmissionSent(TransmissionHandlerArgs args); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.microsoft.applicationinsights.internal.channel; | ||
|
|
||
| import org.apache.http.Header; | ||
|
|
||
| import com.microsoft.applicationinsights.internal.channel.common.Transmission; | ||
|
|
||
| public class TransmissionHandlerArgs { | ||
| private String responseBody; | ||
| public void setResponseBody(String body) { this.responseBody = body;} | ||
| public String getResponseBody() { return this.responseBody;} | ||
|
|
||
|
|
||
| private TransmissionDispatcher transmissionDispatcher; | ||
| public void setTransmissionDispatcher(TransmissionDispatcher dispatcher) { this.transmissionDispatcher = dispatcher;} | ||
| public TransmissionDispatcher getTransmissionDispatcher() { return this.transmissionDispatcher;} | ||
|
|
||
| private Transmission transmission; | ||
| public void setTransmission(Transmission transmission) { this.transmission = transmission;} | ||
| public Transmission getTransmission() { return this.transmission;} | ||
|
|
||
| private int responseCode; | ||
| public void setResponseCode(int code) { this.responseCode = code;} | ||
| public int getResponseCode() { return this.responseCode;} | ||
|
|
||
| private Throwable exception; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @debugthings do we ever get a Throwable here? I think most of the time it would get an instance of Exception class. If you know any place where this could be Throwable please let me know
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the original implementation they catch Throwable for logging. I also consume it here incase there is an Error and want to retry the transmission. Which is then added here And then null checked here |
||
| public void setException(Throwable ex) { this.exception = ex;} | ||
| public Throwable getException() { return this.exception;} | ||
|
|
||
| private Header retryHeader; | ||
| public void setRetryHeader(Header head) { this.retryHeader = head;} | ||
| public Header getRetryHeader() { return this.retryHeader;} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.microsoft.applicationinsights.internal.channel; | ||
|
|
||
|
|
||
| /** | ||
| * Enables the {@link TransmissionPolicyManager} to handle transmission states. | ||
| * <p> | ||
| * This interface extends {@TransmissionHandler} to add the ability to observe when the transmission is completed. | ||
| * @author jamdavi | ||
| * | ||
| */ | ||
| public interface TransmissionHandlerObserver extends TransmissionHandler { | ||
|
|
||
| /** | ||
| * Used to add a {@link TransmissionHandler} to the collection stored by the {@link TransmissionPolicyManager} | ||
| * @param handler The handler to add to the collection. | ||
| */ | ||
| void addTransmissionHandler(TransmissionHandler handler); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.microsoft.applicationinsights.internal.channel.common; | ||
|
|
||
| /** | ||
| * Utility class used by the {@link PartialSuccessHandler} | ||
| * | ||
| * @author jamdavi | ||
| * | ||
| */ | ||
| class BackendResponse { | ||
|
|
||
| int itemsReceived; | ||
| int itemsAccepted; | ||
| Error[] errors; | ||
|
|
||
| class Error { | ||
| public int index; | ||
| public int statusCode; | ||
| public String message; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.microsoft.applicationinsights.internal.channel.common; | ||
|
|
||
| import org.apache.http.HttpStatus; | ||
|
|
||
| import com.microsoft.applicationinsights.internal.channel.TransmissionHandler; | ||
| import com.microsoft.applicationinsights.internal.channel.TransmissionHandlerArgs; | ||
| import com.microsoft.applicationinsights.internal.logger.InternalLogger; | ||
|
|
||
| /** | ||
| * This class implements the retry logic for transmissions with the results of a | ||
| * 408, 500, and 503 result. | ||
| * <p> | ||
| * It does not handle any error codes such as 400, 401, 403, 404, etc. | ||
| * | ||
| * @author jamdavi | ||
| * | ||
| */ | ||
| public class ErrorHandler implements TransmissionHandler { | ||
|
|
||
| private TransmissionPolicyManager transmissionPolicyManager; | ||
|
|
||
| /** | ||
| * Ctor | ||
| * | ||
| * Constructs the ErrorHandler object. | ||
| * | ||
| * @param policy | ||
| * The {@link TransmissionPolicyManager} object that is needed to | ||
| * control the back off policy | ||
| */ | ||
| public ErrorHandler(TransmissionPolicyManager policy) { | ||
| this.transmissionPolicyManager = policy; | ||
| } | ||
|
|
||
| @Override | ||
| public void onTransmissionSent(TransmissionHandlerArgs args) { | ||
|
|
||
| validateTransmissionAndSend(args); | ||
| } | ||
|
|
||
| boolean validateTransmissionAndSend(TransmissionHandlerArgs args) { | ||
| if (args.getTransmission() != null && args.getTransmissionDispatcher() != null) { | ||
| args.getTransmission().incrementNumberOfSends(); | ||
| switch (args.getResponseCode()) { | ||
| case TransmissionSendResult.REQUEST_TIMEOUT: | ||
| case TransmissionSendResult.INTERNAL_SERVER_ERROR: | ||
| case TransmissionSendResult.SERVICE_UNAVAILABLE: | ||
| backoffAndSendTransmission(args); | ||
| return true; | ||
| default: | ||
| InternalLogger.INSTANCE.trace("Http response code %s not handled by %s", args.getResponseCode(), | ||
| this.getClass().getName()); | ||
| return false; | ||
| } | ||
| } else if (args.getException() != null) { | ||
| backoffAndSendTransmission(args); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private void backoffAndSendTransmission(TransmissionHandlerArgs args) { | ||
| this.transmissionPolicyManager.backoff(); | ||
| args.getTransmissionDispatcher().dispatch(args.getTransmission()); | ||
| } | ||
| } |
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.
Do you use gson for actual code? Or this is only for test? If it is only test I would say do not relocate. Relocation usually should be done when it is absolutely necessary.
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.
I added Gson to take care of the parsing of the BackendResponse object that is returned from a partial success. There were no JSON deserializers already included in the solution (at least none that were obvious).
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.
@debugthings makes sense. Gson was used but only for test cases. Since this is now used for production code we do need to shade it. May be we might not need to shade it still. Gradle will pull this as transitive dependency when building. Shading (i.e repacking) to microsoft domain is only needed when it interferes our agent (one example is apache http client). We shade it to avoid instrumenting the calls sdk makes using it. Gson is a widely used library across java and I would rather avoid shading to prevent us from falling into the traps of class path hells.
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.
Fair enough, I also thought that we include it to avoid having to distribute gson with it for instances where this is a drop in addition like a 3rd party.
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.
Yes, that was one reason but, this creates problems of dependency hells if we miss proper shading, and more over if the user uses dependency management, which most do then transient dependencies get pulled down. Further as the SDK grows and we start using more libraries, it would make sense to just shade the one which interfer the operation and leave the rest