Skip to content

Commit 4afdf96

Browse files
Implementing mandatory uncaught exception logging on loader tasks.
1 parent 6adc8b7 commit 4afdf96

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ Current
7474

7575

7676
### Changed:
77+
78+
- [Added mandatory logging for LoadTask errors]
79+
* Finalized `run()` in `LoadTask` with mandatory logging of exceptions.
80+
* Created mandatory runInner to replace run in all `LoadTask` instances
81+
7782
- [Substitute preflight method wildcard character with explicit allowed methods](https://github.com/yahoo/fili/pull/545)
7883
* Modify ResponseCorsFilter Allowed Methods header to explicitly list allowed methods. Some browsers do not support a wildcard header value.
7984

fili-core/src/main/java/com/yahoo/bard/webservice/application/DimensionValueLoadTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public DimensionValueLoadTask(Collection<DimensionValueLoader> dimensionRowProvi
5959
}
6060

6161
@Override
62-
public void run() {
62+
public void runInner() {
6363
dimensionRowProviders.forEach(DimensionValueLoader::load);
6464
// tell all dimensionRowProviders to load
6565
lastRunTimestamp.set(DateTime.now());

fili-core/src/main/java/com/yahoo/bard/webservice/application/LoadTask.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
* @param <V> The type of the result returned by the task associated with this loader.
1919
*/
2020
public abstract class LoadTask<V> implements Runnable {
21+
2122
private static final Logger LOG = LoggerFactory.getLogger(LoadTask.class);
23+
public static final String LOAD_TASK_ERROR_FORMAT = "Exception while running %s: %s";
2224

2325
protected final String loaderName;
2426
protected final long delay;
2527
protected final long period;
2628
protected final boolean isPeriodic;
2729
protected ScheduledFuture<?> future;
2830

31+
private boolean failed = false;
32+
2933
/**
3034
* Creates a one-off loader.
3135
*
@@ -50,8 +54,22 @@ public LoadTask(String loaderName, long delay, long period) {
5054
this.isPeriodic = period > 0;
5155
}
5256

57+
/**
58+
* Internal implementation of {@link LoadTask#run()} task that will be used so that {@link LoadTask#run()} can
59+
* add mandatory exception handling.
60+
*/
61+
public abstract void runInner();
62+
5363
@Override
54-
public abstract void run();
64+
public final void run() {
65+
try {
66+
runInner();
67+
} catch (RuntimeException t) {
68+
LOG.error(String.format(LOAD_TASK_ERROR_FORMAT, getName(), t.getMessage()), t);
69+
failed = true;
70+
throw t;
71+
}
72+
};
5573

5674
/**
5775
* Return the name of this loader.
@@ -157,4 +175,8 @@ public void invoke(Throwable error) {
157175
LOG.error("{}: Async request to druid failed:", getName(), error);
158176
}
159177
}
178+
179+
public boolean isFailed() {
180+
return failed;
181+
}
160182
}

fili-core/src/main/java/com/yahoo/bard/webservice/metadata/DataSourceMetadataLoadTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public DataSourceMetadataLoadTask(
9797
}
9898

9999
@Override
100-
public void run() {
100+
public void runInner() {
101101
physicalTableDictionary.values().stream()
102102
.map(PhysicalTable::getDataSourceNames)
103103
.flatMap(Set::stream)

fili-core/src/test/groovy/com/yahoo/bard/webservice/application/LoadTaskSpec.groovy

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ package com.yahoo.bard.webservice.application
55
import static spock.util.matcher.HamcrestMatchers.closeTo
66
import static spock.util.matcher.HamcrestSupport.expect
77

8+
import com.yahoo.bard.webservice.data.dimension.TimeoutException
89
import com.yahoo.bard.webservice.druid.client.FailureCallback
910
import com.yahoo.bard.webservice.druid.client.HttpErrorCallback
11+
import com.yahoo.bard.webservice.logging.TestLogAppender
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper
1014

1115
import spock.lang.Ignore
1216
import spock.lang.Specification
@@ -18,6 +22,8 @@ import java.util.concurrent.TimeUnit
1822
@Timeout(30) // Fail test if hangs
1923
class LoadTaskSpec extends Specification {
2024

25+
TestLogAppender logAppender = new TestLogAppender()
26+
2127
// All times are in millis unless specified otherwise
2228
int expectedDelay = 500
2329
int expectedPeriod = 500
@@ -42,7 +48,7 @@ class LoadTaskSpec extends Specification {
4248
}
4349

4450
@Override
45-
void run() {
51+
void runInner() {
4652
start = System.currentTimeMillis()
4753
sleep(expectedDuration)
4854
end = System.currentTimeMillis()
@@ -60,7 +66,7 @@ class LoadTaskSpec extends Specification {
6066
}
6167

6268
@Override
63-
void run() {
69+
void runInner() {
6470
start = System.currentTimeMillis()
6571
sleep(expectedDuration)
6672
end = System.currentTimeMillis()
@@ -231,4 +237,42 @@ class LoadTaskSpec extends Specification {
231237
cleanup:
232238
scheduler.shutdownNow()
233239
}
240+
241+
class FailingTestLoadTask extends LoadTaskSpec.OneOffTestLoadTask {
242+
243+
244+
public static final String UNIQUE_VALUE_1 = "UNIQUE_VALUE_1"
245+
246+
FailingTestLoadTask(long delay) {
247+
super(delay)
248+
}
249+
250+
@Override
251+
void runInner() {
252+
throw new TimeoutException(UNIQUE_VALUE_1);
253+
}
254+
}
255+
256+
257+
def "Test error gets logged on failed test"() {
258+
setup:
259+
setup: "Instantiate a task scheduler"
260+
ObjectMapper MAPPER = new ObjectMappersSuite().getMapper()
261+
262+
263+
TaskScheduler scheduler = new TaskScheduler(2)
264+
LoadTask<Boolean> loader = new FailingTestLoadTask(expectedDelay)
265+
loader.setFuture(
266+
scheduler.schedule(
267+
loader,
268+
1,
269+
TimeUnit.MILLISECONDS
270+
)
271+
)
272+
Thread.sleep(10)
273+
274+
expect:
275+
loader.failed
276+
logAppender.getMessages().find { it.contains(/UNIQUE_VALUE_1/) }
277+
}
234278
}

0 commit comments

Comments
 (0)