Skip to content

Commit 2319268

Browse files
authored
Merge pull request #1253 from microsoft/trask/fix-backoff-retries
Fix backoff retries
2 parents 6a45b92 + 3830733 commit 2319268

File tree

6 files changed

+4
-332
lines changed

6 files changed

+4
-332
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ final class ExponentialBackOffTimesPolicy implements BackOffTimesPolicy {
5151
FIVE_SECONDS_IN_MILLIS,
5252
FOUR_MINUTES_IN_MILLIS,
5353
FIVE_SECONDS_IN_MILLIS,
54-
SIX_MINUTES_IN_MILLIS,
55-
FIVE_SECONDS_IN_MILLIS
54+
SIX_MINUTES_IN_MILLIS
5655
};
5756

5857
@Override

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

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package com.microsoft.applicationinsights.internal.channel.common;
2323

24-
import java.util.concurrent.TimeUnit;
2524
import java.util.concurrent.locks.Condition;
2625
import java.util.concurrent.locks.ReentrantLock;
2726

@@ -78,50 +77,6 @@ public void onDoneSending() {
7877
currentBackOffIndex = -1;
7978
}
8079

81-
/**
82-
* The calling thread will be suspended for an amount of time that is
83-
* set in the 'backOffTimeoutsInSeconds' array by using its index 'currentBackOffIndex'.
84-
*
85-
* @return True if the thread completed the suspension time as expected, in which case
86-
* the caller should re-try to send the Transmission.
87-
* False, in which case the caller should 'abandon' this Transmission and move to the next one,
88-
* if:
89-
* 1. The instance was marked as non-active before the thread started to wait.
90-
* 2. The thread was signaled to stop while was waiting.
91-
* 3. The thread was interrupted while was waiting.
92-
* 4. The thread has exhausted all the back-off timeouts
93-
*/
94-
public boolean backOff() {
95-
try {
96-
lock.lock();
97-
++currentBackOffIndex;
98-
if (currentBackOffIndex == backOffTimeoutsInMillis.length) {
99-
currentBackOffIndex = -1;
100-
101-
// Exhausted the back-offs
102-
return false;
103-
}
104-
105-
if (!instanceIsActive) {
106-
return false;
107-
}
108-
109-
try {
110-
long millisecondsToWait = backOffTimeoutsInMillis[currentBackOffIndex];
111-
if (millisecondsToWait > BackOffTimesPolicy.MIN_TIME_TO_BACK_OFF_IN_MILLS) {
112-
millisecondsToWait += addMilliseconds;
113-
}
114-
backOffCondition.await(millisecondsToWait, TimeUnit.MILLISECONDS);
115-
return instanceIsActive;
116-
} catch (InterruptedException e) {
117-
Thread.currentThread().interrupt();
118-
return false;
119-
}
120-
} finally {
121-
lock.unlock();
122-
}
123-
}
124-
12580
/**
12681
* Increment the current back off amount or resets the counter if needed.
12782
* <p>
@@ -132,13 +87,8 @@ public boolean backOff() {
13287
public long backOffTimerValue() {
13388
try {
13489
lock.lock();
135-
++currentBackOffIndex;
136-
if (currentBackOffIndex == backOffTimeoutsInMillis.length) {
137-
currentBackOffIndex = -1;
138-
139-
// Exhausted the back-offs
140-
return -1;
141-
}
90+
// when the last backoff index is hit, stay there until backoff is reset
91+
currentBackOffIndex = Math.min(currentBackOffIndex + 1, backOffTimeoutsInMillis.length - 1);
14292

14393
if (!instanceIsActive) {
14494
return 0;

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ public long backOffCurrentSenderThreadValue() {
6969
return currentThreadData.backOffTimerValue();
7070
}
7171

72-
public boolean backOffCurrentSenderThread() {
73-
SenderThreadLocalBackOffData currentThreadData = this.get();
74-
return currentThreadData.backOff();
75-
}
76-
7772
public synchronized void stopAllSendersBackOffActivities() {
7873
for (SenderThreadLocalBackOffData sender : allSendersData) {
7974
sender.stop();

core/src/test/java/com/microsoft/applicationinsights/internal/channel/common/ExponentialBackOffTimesPolicyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class ExponentialBackOffTimesPolicyTest {
3535
public void testBackOffs() {
3636
long[] backOffs = new ExponentialBackOffTimesPolicy().getBackOffTimeoutsInMillis();
3737
assertNotNull(backOffs);
38-
assertTrue(backOffs.length % 2 == 1);
38+
assertTrue(backOffs.length % 2 == 0);
3939
int couples = backOffs.length / 2;
4040
long lastEventValue = BackOffTimesPolicy.MIN_TIME_TO_BACK_OFF_IN_MILLS;
4141
for (int i = 0; i < couples; ++i) {

core/src/test/java/com/microsoft/applicationinsights/internal/channel/common/SenderThreadLocalDataTest.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,58 +48,17 @@ public void testStateAfterCtor() {
4848
assertFalse(sender.isTryingToSend());
4949
}
5050

51-
@Test
52-
public void testMultipleOnFailedSending() {
53-
final SenderThreadLocalBackOffData sender = createSenderThreadLocalData(new long[] {1000,2000,1000});
54-
verifyBackOff(sender, 3, 4);
55-
}
56-
5751
@Test
5852
public void testOnDoneSending() {
5953
final SenderThreadLocalBackOffData sender = createSenderThreadLocalData(new long[] {1000});
6054
verifyOnDoneSending(sender);
6155
}
6256

63-
@Test
64-
public void testDoneSendingAfterFailedSending() {
65-
final SenderThreadLocalBackOffData sender = createSenderThreadLocalData(new long[]{1000});
66-
verifyBackOff(sender, 1, 1);
67-
verifyOnDoneSending(sender);
68-
}
69-
70-
@Test
71-
public void testStopWhileWaiting() throws InterruptedException {
72-
final SenderThreadLocalBackOffData sender = createSenderThreadLocalData(new long[]{10000});
73-
Thread thread = new Thread(new Runnable() {
74-
@Override
75-
public void run() {
76-
sender.stop();
77-
}
78-
});
79-
thread.setDaemon(true);
80-
thread.start();
81-
sender.backOff();
82-
verifyOnDoneSending(sender);
83-
thread.join();
84-
}
85-
8657
private SenderThreadLocalBackOffData createSenderThreadLocalData(long[] backOffs) {
8758
SenderThreadLocalBackOffData sender = new SenderThreadLocalBackOffData(backOffs, 0);
8859
return sender;
8960
}
9061

91-
private static void verifyBackOff(SenderThreadLocalBackOffData sender, int backOffTimes, int expectedSeconds) {
92-
long started = System.nanoTime();
93-
for (int i = 0; i < backOffTimes; ++i) {
94-
sender.backOff();
95-
}
96-
97-
int elapsed = (int)((double)(System.nanoTime() - started) / 1000000000.0);
98-
assertTrue(String.format("BackOff lasted %d which is less than expected %d", elapsed, expectedSeconds), elapsed >= expectedSeconds);
99-
assertTrue(String.format("BackOff lasted %d which is more than expected %d", elapsed, expectedSeconds), elapsed <= expectedSeconds + 2);
100-
assertTrue(sender.isTryingToSend());
101-
}
102-
10362
private static void verifyOnDoneSending(SenderThreadLocalBackOffData sender) {
10463
sender.onDoneSending();
10564

core/src/test/java/com/microsoft/applicationinsights/internal/channel/common/SenderThreadsBackOffManagerTest.java

Lines changed: 0 additions & 231 deletions
This file was deleted.

0 commit comments

Comments
 (0)