Skip to content

Commit fb8e206

Browse files
committed
Fix initial wait of ExponentialDelayReconnector and address unit test failures
1 parent 3812128 commit fb8e206

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/main/java/org/fluentd/logger/sender/ExponentialDelayReconnector.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* function of the number of connection errors.
88
*/
99
public class ExponentialDelayReconnector implements Reconnector {
10+
// Visible for test
11+
public static final double WAIT_MILLIS = 500; // Start wait is 500ms
1012

11-
private double waitMillis = 50; // Start wait is 50ms
13+
private static final double WAIT_INCR_RATE = 1.5;
1214

13-
private double waitIncrRate = 1.5;
14-
15-
private double waitMaxMillis = 60 * 1000; // Max wait is 1 minute
15+
private static final double WAIT_MAX_MILLIS = 60 * 1000; // Max wait is 1 minute
1616

1717
private int waitMaxCount;
1818

@@ -24,12 +24,12 @@ public ExponentialDelayReconnector() {
2424
}
2525

2626
private int getWaitMaxCount() {
27-
double r = waitMaxMillis / waitMillis;
27+
double r = WAIT_MAX_MILLIS / WAIT_MILLIS;
2828
for (int j = 1; j <= 100; j++) {
29-
if (r < waitIncrRate) {
29+
if (r < WAIT_INCR_RATE) {
3030
return j + 1;
3131
}
32-
r = r / waitIncrRate;
32+
r = r / WAIT_INCR_RATE;
3333
}
3434
return 100;
3535
}
@@ -57,9 +57,9 @@ public boolean enableReconnection(long timestamp) {
5757

5858
double suppressMillis;
5959
if (size < waitMaxCount) {
60-
suppressMillis = waitMillis * Math.pow(waitIncrRate, size - 1);
60+
suppressMillis = WAIT_MILLIS * Math.pow(WAIT_INCR_RATE, size - 1);
6161
} else {
62-
suppressMillis = waitMaxMillis;
62+
suppressMillis = WAIT_MAX_MILLIS;
6363
}
6464

6565
return (timestamp - errorHistory.getLast()) >= suppressMillis;

src/test/java/org/fluentd/logger/TestFluentLogger.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.fluentd.logger.errorhandler.ErrorHandler;
44
import org.fluentd.logger.sender.Event;
5+
import org.fluentd.logger.sender.ExponentialDelayReconnector;
56
import org.fluentd.logger.sender.NullSender;
67
import org.fluentd.logger.sender.Sender;
78
import org.fluentd.logger.util.MockFluentd;
9+
import org.junit.Before;
810
import org.junit.Test;
911
import org.msgpack.MessagePack;
1012
import org.msgpack.unpacker.Unpacker;
@@ -47,6 +49,12 @@ public void join() throws InterruptedException {
4749
}
4850
}
4951

52+
@Before
53+
public void setUp() {
54+
// To remove garbage loggers from org.fluentd.logger.FluentLoggerFactory.loggers...
55+
System.gc();
56+
}
57+
5058

5159
@Test
5260
public void testNormal01() throws Exception {
@@ -274,7 +282,7 @@ public void handleNetworkError(IOException ex) {
274282
assertTrue(lastError.get() instanceof IOException);
275283
lastError.set(null); // Clear the last error
276284
assertFalse(logger.isConnected());
277-
TimeUnit.MILLISECONDS.sleep(100);
285+
TimeUnit.MILLISECONDS.sleep((long) (ExponentialDelayReconnector.WAIT_MILLIS * 1.5));
278286
}
279287

280288
// the logger shouldn't call the error handler after calling removeErrorHandler()
@@ -311,6 +319,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
311319
threadManager.submit(fluentd2);
312320
fluentd2.waitUntilReady();
313321

322+
TimeUnit.MILLISECONDS.sleep((long) (ExponentialDelayReconnector.WAIT_MILLIS * 1.5));
314323
{
315324
Map<String, Object> data = new HashMap<String, Object>();
316325
data.put("k5", "v5");

0 commit comments

Comments
 (0)