|
39 | 39 | import com.netflix.hollow.core.util.DefaultHashCodeFinder;
|
40 | 40 | import com.netflix.hollow.core.util.HollowObjectHashCodeFinder;
|
41 | 41 | import com.netflix.hollow.tools.history.HollowHistory;
|
| 42 | +import org.slf4j.MDC; |
| 43 | + |
42 | 44 | import java.io.File;
|
43 | 45 | import java.io.IOException;
|
44 | 46 | import java.io.InputStream;
|
@@ -258,25 +260,36 @@ public void triggerAsyncRefresh() {
|
258 | 260 | */
|
259 | 261 | public void triggerAsyncRefreshWithDelay(int delayMillis) {
|
260 | 262 | final long targetBeginTime = System.currentTimeMillis() + delayMillis;
|
| 263 | + // Capture MDC context on the original thread |
| 264 | + final Map<String, String> mdcContext = MDC.getCopyOfContextMap(); |
261 | 265 |
|
262 | 266 | refreshExecutor.execute(() -> {
|
263 |
| - try { |
264 |
| - long delay = targetBeginTime - System.currentTimeMillis(); |
265 |
| - if (delay > 0) |
266 |
| - Thread.sleep(delay); |
267 |
| - } catch (InterruptedException e) { |
268 |
| - // Interrupting, such as shutting down the executor pool, |
269 |
| - // cancels the trigger |
270 |
| - LOG.log(Level.INFO, "Async refresh interrupted before trigger, refresh cancelled", e); |
271 |
| - return; |
| 267 | + // Set MDC context on the new thread |
| 268 | + if (mdcContext != null) { |
| 269 | + MDC.setContextMap(mdcContext); |
272 | 270 | }
|
273 |
| - |
274 | 271 | try {
|
275 |
| - triggerRefresh(); |
276 |
| - } catch (Error | RuntimeException e) { |
277 |
| - // Ensure exceptions are propagated to the executor |
278 |
| - LOG.log(Level.SEVERE, "Async refresh failed", e); |
279 |
| - throw e; |
| 272 | + try { |
| 273 | + long delay = targetBeginTime - System.currentTimeMillis(); |
| 274 | + if (delay > 0) |
| 275 | + Thread.sleep(delay); |
| 276 | + } catch (InterruptedException e) { |
| 277 | + // Interrupting, such as shutting down the executor pool, |
| 278 | + // cancels the trigger |
| 279 | + LOG.log(Level.INFO, "Async refresh interrupted before trigger, refresh cancelled", e); |
| 280 | + return; |
| 281 | + } |
| 282 | + |
| 283 | + try { |
| 284 | + triggerRefresh(); |
| 285 | + } catch (Error | RuntimeException e) { |
| 286 | + // Ensure exceptions are propagated to the executor |
| 287 | + LOG.log(Level.SEVERE, "Async refresh failed", e); |
| 288 | + throw e; |
| 289 | + } |
| 290 | + } finally { |
| 291 | + // clear MDC context when entire task is done |
| 292 | + MDC.clear(); |
280 | 293 | }
|
281 | 294 | });
|
282 | 295 | }
|
|
0 commit comments