Skip to content

Commit 18b6fe2

Browse files
debugthingsgrlima
authored andcommitted
Bugfix against retry logic (#576)
* Refactor * BUGFIX Logic would never backoff After adding the instant retry amount logic to the code this line of code could cause the transmissions to not back off. * Changes requested
1 parent 99dbf73 commit 18b6fe2

File tree

2 files changed

+58
-61
lines changed

2 files changed

+58
-61
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionNetworkOutput.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
*
5151
* Created by gupele on 12/18/2014.
5252
*/
53-
public final class TransmissionNetworkOutput implements TransmissionOutput {
54-
private static final int MAX_RESEND = 3;
53+
public final class TransmissionNetworkOutput implements TransmissionOutput {
5554
private final static String CONTENT_TYPE_HEADER = "Content-Type";
5655
private final static String CONTENT_ENCODING_HEADER = "Content-Encoding";
5756
private final static String RESPONSE_THROTTLING_HEADER = "Retry-After";
@@ -189,10 +188,8 @@ public boolean send(Transmission transmission) {
189188
respString = EntityUtils.toString(respEntity);
190189
retryAfterHeader = response.getFirstHeader(RESPONSE_THROTTLING_HEADER);
191190

192-
// After the third time through this dispatcher we should reset the counter and
193-
// then fail to second TransmissionOutput
194-
if (code > HttpStatus.SC_PARTIAL_CONTENT && transmission.getNumberOfSends() >= MAX_RESEND) {
195-
transmission.setNumberOfSends(0);
191+
// After we reach our instant retry limit we should fail to second TransmissionOutput
192+
if (code > HttpStatus.SC_PARTIAL_CONTENT && transmission.getNumberOfSends() > this.transmissionPolicyManager.getMaxInstantRetries()) {
196193
return false;
197194
} else if (code == HttpStatus.SC_OK) {
198195
// If we've completed then clear the back off flags as the channel does not need
@@ -231,7 +228,7 @@ public boolean send(Transmission transmission) {
231228
}
232229
httpClient.dispose(response);
233230

234-
if (code != HttpStatus.SC_OK && transmission.getNumberOfSends() < MAX_RESEND) {
231+
if (code != HttpStatus.SC_OK) {
235232
// Invoke the listeners for handling things like errors
236233
// The listeners will handle the back off logic as well as the dispatch
237234
// operation

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionPolicyManager.java

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252
*/
5353
public final class TransmissionPolicyManager implements Stoppable, TransmissionHandlerObserver {
5454

55-
private int INSTANT_RETRY_AMOUNT = 3; // Should always be set by the creator of this class
56-
private int INSTANT_RETRY_MAX = 10; // Stops us from getting into an endless loop
57-
58-
// Current thread backoff manager
59-
private SenderThreadsBackOffManager backoffManager;
60-
61-
// List of transmission policies implemented as handlers
62-
private List<TransmissionHandler> transmissionHandlers;
63-
55+
private int instantRetryAmount = 3; // Should always be set by the creator of this class
56+
private final int INSTANT_RETRY_MAX = 10; // Stops us from getting into an endless loop
57+
58+
// Current thread backoff manager
59+
private SenderThreadsBackOffManager backoffManager;
60+
61+
// List of transmission policies implemented as handlers
62+
private List<TransmissionHandler> transmissionHandlers;
63+
6464
// The future date the the transmission is blocked
6565
private Date suspensionDate;
6666

@@ -95,7 +95,7 @@ public void run() {
9595

9696
/**
9797
* Create the {@link TransmissionPolicyManager} and set the ability to throttle.
98-
* @param throttlingIsEnabled Set whether the {@link TransmissionPolicyManager} can be throttled.
98+
* @param throttlingIsEnabled Set whether the {@link TransmissionPolicyManager} can be throttled.
9999
*/
100100
public TransmissionPolicyManager(boolean throttlingIsEnabled) {
101101
suspensionDate = null;
@@ -104,32 +104,32 @@ public TransmissionPolicyManager(boolean throttlingIsEnabled) {
104104
this.backoffManager = new SenderThreadsBackOffManager(new ExponentialBackOffTimesPolicy());
105105
}
106106

107-
/**
107+
/**
108108
* Suspend the transmission thread according to the current back off policy.
109109
*/
110110
public void backoff() {
111-
policyState.setCurrentState(TransmissionPolicy.BACKOFF);
112-
long backOffMillis = backoffManager.backOffCurrentSenderThreadValue();
113-
if (backOffMillis > 0)
111+
policyState.setCurrentState(TransmissionPolicy.BACKOFF);
112+
long backOffMillis = backoffManager.backOffCurrentSenderThreadValue();
113+
if (backOffMillis > 0)
114114
{
115115
long backOffSeconds = backOffMillis / 1000;
116116
InternalLogger.INSTANCE.info("App is throttled, telemetry will be blocked for %s seconds.", backOffSeconds);
117117
this.suspendInSeconds(TransmissionPolicy.BACKOFF, backOffSeconds);
118-
}
118+
}
119119
}
120-
120+
121121
/**
122122
* Clear the current thread state and and reset the back off counter.
123123
*/
124124
public void clearBackoff() {
125-
policyState.setCurrentState(TransmissionPolicy.UNBLOCKED);
125+
policyState.setCurrentState(TransmissionPolicy.UNBLOCKED);
126126
backoffManager.onDoneSending();
127127
InternalLogger.INSTANCE.info("Backoff has been reset.");
128128
}
129-
129+
130130
/**
131131
* Suspend this transmission thread using the specified policy
132-
* @param policy The {@link TransmissionPolicy} to use for suspension
132+
* @param policy The {@link TransmissionPolicy} to use for suspension
133133
* @param suspendInSeconds The number of seconds to suspend.
134134
*/
135135
public void suspendInSeconds(TransmissionPolicy policy, long suspendInSeconds) {
@@ -162,12 +162,12 @@ public TransmissionPolicyStateFetcher getTransmissionPolicyState() {
162162

163163
private synchronized void doSuspend(TransmissionPolicy policy, long suspendInSeconds) {
164164
try {
165-
if (policy == TransmissionPolicy.UNBLOCKED ) {
165+
if (policy == TransmissionPolicy.UNBLOCKED) {
166166
return;
167167
}
168-
168+
169169
Date date = Calendar.getInstance().getTime();
170-
date.setTime(date.getTime() + 1000 * suspendInSeconds);
170+
date.setTime(date.getTime() + (1000 * suspendInSeconds));
171171
if (this.suspensionDate != null) {
172172
long diff = date.getTime() - suspensionDate.getTime();
173173
if (diff <= 0) {
@@ -214,36 +214,36 @@ public Thread newThread(Runnable r) {
214214

215215
SDKShutdownActivity.INSTANCE.register(this);
216216
}
217-
218-
@Override
219-
public void onTransmissionSent(TransmissionHandlerArgs transmissionArgs) {
220-
for (TransmissionHandler handler : this.transmissionHandlers) {
221-
handler.onTransmissionSent(transmissionArgs);
222-
}
223-
}
224-
225-
@Override
226-
public void addTransmissionHandler(TransmissionHandler handler) {
227-
if(handler != null) {
228-
this.transmissionHandlers.add(handler);
229-
}
230-
}
231-
232-
/**
233-
* Set the number of retries before performing a back off operation.
234-
* @param maxInstantRetries Number of retries
235-
*/
236-
public void setMaxInstantRetries(int maxInstantRetries) {
237-
if (maxInstantRetries >= 0 && maxInstantRetries < INSTANT_RETRY_MAX) {
238-
INSTANT_RETRY_AMOUNT = maxInstantRetries;
239-
}
240-
}
241-
242-
/**
243-
* Get the number of retries before performing a back off operation.
244-
* @return Number of retries
245-
*/
246-
public int getMaxInstantRetries() {
247-
return INSTANT_RETRY_AMOUNT;
248-
}
217+
218+
@Override
219+
public void onTransmissionSent(TransmissionHandlerArgs transmissionArgs) {
220+
for (TransmissionHandler handler : this.transmissionHandlers) {
221+
handler.onTransmissionSent(transmissionArgs);
222+
}
223+
}
224+
225+
@Override
226+
public void addTransmissionHandler(TransmissionHandler handler) {
227+
if (handler != null) {
228+
this.transmissionHandlers.add(handler);
229+
}
230+
}
231+
232+
/**
233+
* Set the number of retries before performing a back off operation.
234+
* @param maxInstantRetries Number of retries
235+
*/
236+
public void setMaxInstantRetries(int maxInstantRetries) {
237+
if (maxInstantRetries > 0 && maxInstantRetries < INSTANT_RETRY_MAX) {
238+
instantRetryAmount = maxInstantRetries;
239+
}
240+
}
241+
242+
/**
243+
* Get the number of retries before performing a back off operation.
244+
* @return Number of retries
245+
*/
246+
public int getMaxInstantRetries() {
247+
return instantRetryAmount;
248+
}
249249
}

0 commit comments

Comments
 (0)