From 9999b2f05232f653fcb38ab06dae402183c608e5 Mon Sep 17 00:00:00 2001 From: Nayanika Upadhyay Date: Tue, 5 Sep 2023 00:07:24 -0700 Subject: [PATCH] passing MDC context from Cinder --- hollow-diff-ui/dependencies.lock | 18 +++++++ hollow-explorer-ui/dependencies.lock | 18 +++++++ hollow-jsonadapter/dependencies.lock | 12 +++++ hollow-perf/dependencies.lock | 18 +++++++ hollow-test/dependencies.lock | 12 +++++ hollow-ui-tools/dependencies.lock | 18 +++++++ hollow-zenoadapter/dependencies.lock | 12 +++++ hollow/build.gradle | 1 + hollow/dependencies.lock | 19 +++++++ .../api/client/HollowAnnouncementWatcher.java | 12 ++++- .../hollow/api/consumer/HollowConsumer.java | 43 ++++++++++------ .../HollowFilesystemAnnouncementWatcher.java | 51 ++++++++++++------- .../api/producer/AbstractHollowProducer.java | 10 ++++ 13 files changed, 210 insertions(+), 34 deletions(-) diff --git a/hollow-diff-ui/dependencies.lock b/hollow-diff-ui/dependencies.lock index 1bcc7c3e59..15dce0c711 100644 --- a/hollow-diff-ui/dependencies.lock +++ b/hollow-diff-ui/dependencies.lock @@ -68,6 +68,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -145,6 +151,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "toolsCompileClasspath": { @@ -222,6 +234,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-explorer-ui/dependencies.lock b/hollow-explorer-ui/dependencies.lock index 2c4af747cd..8c3018694b 100644 --- a/hollow-explorer-ui/dependencies.lock +++ b/hollow-explorer-ui/dependencies.lock @@ -58,6 +58,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -125,6 +131,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "toolsCompileClasspath": { @@ -192,6 +204,12 @@ "com.netflix.hollow:hollow-ui-tools" ], "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-jsonadapter/dependencies.lock b/hollow-jsonadapter/dependencies.lock index ec04fd5aa8..0d77a4cc0d 100644 --- a/hollow-jsonadapter/dependencies.lock +++ b/hollow-jsonadapter/dependencies.lock @@ -31,6 +31,12 @@ }, "commons-lang:commons-lang": { "locked": "2.6" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -71,6 +77,12 @@ }, "junit:junit": { "locked": "4.11" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-perf/dependencies.lock b/hollow-perf/dependencies.lock index 8ee43c9219..fa6832735e 100644 --- a/hollow-perf/dependencies.lock +++ b/hollow-perf/dependencies.lock @@ -41,6 +41,12 @@ }, "org.openjdk.jmh:jmh-generator-bytecode": { "locked": "1.25" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "runtimeClasspath": { @@ -49,6 +55,12 @@ }, "org.openjdk.jmh:jmh-core": { "locked": "1.21" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -65,6 +77,12 @@ }, "org.openjdk.jmh:jmh-core": { "locked": "1.21" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-test/dependencies.lock b/hollow-test/dependencies.lock index 680481f9f2..d7b0560d17 100644 --- a/hollow-test/dependencies.lock +++ b/hollow-test/dependencies.lock @@ -7,6 +7,12 @@ "runtimeClasspath": { "com.netflix.hollow:hollow": { "project": true + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -23,6 +29,12 @@ }, "junit:junit": { "locked": "4.11" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-ui-tools/dependencies.lock b/hollow-ui-tools/dependencies.lock index b258c3161c..60cb4a933d 100644 --- a/hollow-ui-tools/dependencies.lock +++ b/hollow-ui-tools/dependencies.lock @@ -37,6 +37,12 @@ }, "org.apache.velocity:velocity-engine-core": { "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -83,6 +89,12 @@ }, "org.apache.velocity:velocity-engine-core": { "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "toolsCompileClasspath": { @@ -129,6 +141,12 @@ }, "org.apache.velocity:velocity-engine-core": { "locked": "2.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow-zenoadapter/dependencies.lock b/hollow-zenoadapter/dependencies.lock index c3029081b2..bd7d89a56e 100644 --- a/hollow-zenoadapter/dependencies.lock +++ b/hollow-zenoadapter/dependencies.lock @@ -13,6 +13,12 @@ }, "com.netflix.zeno:netflix-zeno": { "locked": "2.22.3" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } }, "testCompileClasspath": { @@ -35,6 +41,12 @@ }, "junit:junit": { "locked": "4.11" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow/build.gradle b/hollow/build.gradle index b804536e7f..5549a1a07e 100644 --- a/hollow/build.gradle +++ b/hollow/build.gradle @@ -6,6 +6,7 @@ dependencies { testImplementation 'org.mockito:mockito-core:2.15.0' testImplementation 'com.github.spotbugs:spotbugs:4.2.1' testImplementation 'org.assertj:assertj-core:3.15.0' + implementation group: 'org.slf4j', name: 'jul-to-slf4j', version: '2.0.9' } tasks.withType(JavaCompile).configureEach { diff --git a/hollow/dependencies.lock b/hollow/dependencies.lock index 6ee279e66a..51e926b8b0 100644 --- a/hollow/dependencies.lock +++ b/hollow/dependencies.lock @@ -1,4 +1,14 @@ { + "compileClasspath": { + "org.slf4j:jul-to-slf4j": { + "locked": "2.0.9" + } + }, + "runtimeClasspath": { + "org.slf4j:jul-to-slf4j": { + "locked": "2.0.9" + } + }, "testCompileClasspath": { "com.github.spotbugs:spotbugs": { "locked": "4.2.1" @@ -14,6 +24,9 @@ }, "org.mockito:mockito-core": { "locked": "2.15.0" + }, + "org.slf4j:jul-to-slf4j": { + "locked": "2.0.9" } }, "testRuntimeClasspath": { @@ -37,6 +50,12 @@ }, "org.mockito:mockito-core": { "locked": "2.15.0" + }, + "org.slf4j:jul-to-slf4j": { + "firstLevelTransitive": [ + "com.netflix.hollow:hollow" + ], + "locked": "2.0.9" } } } \ No newline at end of file diff --git a/hollow/src/main/java/com/netflix/hollow/api/client/HollowAnnouncementWatcher.java b/hollow/src/main/java/com/netflix/hollow/api/client/HollowAnnouncementWatcher.java index 983e911de6..1ffa6025b8 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/client/HollowAnnouncementWatcher.java +++ b/hollow/src/main/java/com/netflix/hollow/api/client/HollowAnnouncementWatcher.java @@ -20,6 +20,9 @@ import com.netflix.hollow.api.consumer.HollowConsumer; import com.netflix.hollow.core.HollowConstants; +import org.slf4j.MDC; + +import java.util.Map; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -129,9 +132,14 @@ public void triggerAsyncRefreshWithRandomDelay(int maxDelayMillis) { public void triggerAsyncRefreshWithDelay(int delayMillis) { final HollowClient client = this.client; final long targetBeginTime = System.currentTimeMillis() + delayMillis; - + // Capture MDC context on the original thread + final Map mdcContext = MDC.getCopyOfContextMap(); refreshExecutor.execute(new Runnable() { public void run() { + // Set MDC context on the new thread + if (mdcContext != null) { + MDC.setContextMap(mdcContext); + } try { long delay = targetBeginTime - System.currentTimeMillis(); if(delay > 0) @@ -139,6 +147,8 @@ public void run() { client.triggerRefresh(); } catch(Throwable th) { log.log(Level.SEVERE, "Async refresh failed", th); + } finally { + MDC.clear(); } } }); diff --git a/hollow/src/main/java/com/netflix/hollow/api/consumer/HollowConsumer.java b/hollow/src/main/java/com/netflix/hollow/api/consumer/HollowConsumer.java index 3b560aa4b2..19ea7ceefd 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/consumer/HollowConsumer.java +++ b/hollow/src/main/java/com/netflix/hollow/api/consumer/HollowConsumer.java @@ -39,6 +39,8 @@ import com.netflix.hollow.core.util.DefaultHashCodeFinder; import com.netflix.hollow.core.util.HollowObjectHashCodeFinder; import com.netflix.hollow.tools.history.HollowHistory; +import org.slf4j.MDC; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -258,25 +260,36 @@ public void triggerAsyncRefresh() { */ public void triggerAsyncRefreshWithDelay(int delayMillis) { final long targetBeginTime = System.currentTimeMillis() + delayMillis; + // Capture MDC context on the original thread + final Map mdcContext = MDC.getCopyOfContextMap(); refreshExecutor.execute(() -> { - try { - long delay = targetBeginTime - System.currentTimeMillis(); - if (delay > 0) - Thread.sleep(delay); - } catch (InterruptedException e) { - // Interrupting, such as shutting down the executor pool, - // cancels the trigger - LOG.log(Level.INFO, "Async refresh interrupted before trigger, refresh cancelled", e); - return; + // Set MDC context on the new thread + if (mdcContext != null) { + MDC.setContextMap(mdcContext); } - try { - triggerRefresh(); - } catch (Error | RuntimeException e) { - // Ensure exceptions are propagated to the executor - LOG.log(Level.SEVERE, "Async refresh failed", e); - throw e; + try { + long delay = targetBeginTime - System.currentTimeMillis(); + if (delay > 0) + Thread.sleep(delay); + } catch (InterruptedException e) { + // Interrupting, such as shutting down the executor pool, + // cancels the trigger + LOG.log(Level.INFO, "Async refresh interrupted before trigger, refresh cancelled", e); + return; + } + + try { + triggerRefresh(); + } catch (Error | RuntimeException e) { + // Ensure exceptions are propagated to the executor + LOG.log(Level.SEVERE, "Async refresh failed", e); + throw e; + } + } finally { + // clear MDC context when entire task is done + MDC.clear(); } }); } diff --git a/hollow/src/main/java/com/netflix/hollow/api/consumer/fs/HollowFilesystemAnnouncementWatcher.java b/hollow/src/main/java/com/netflix/hollow/api/consumer/fs/HollowFilesystemAnnouncementWatcher.java index a7f41a1463..1803097793 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/consumer/fs/HollowFilesystemAnnouncementWatcher.java +++ b/hollow/src/main/java/com/netflix/hollow/api/consumer/fs/HollowFilesystemAnnouncementWatcher.java @@ -22,6 +22,8 @@ import com.netflix.hollow.api.consumer.HollowConsumer; import com.netflix.hollow.api.producer.fs.HollowFilesystemAnnouncer; +import org.slf4j.MDC; + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -30,6 +32,7 @@ import java.nio.file.Path; import java.nio.file.attribute.FileTime; import java.util.List; +import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -127,28 +130,40 @@ static class Watch implements Runnable { @Override public void run() { + // Capture MDC context on the original thread + final Map mdcContext = MDC.getCopyOfContextMap(); try { - HollowFilesystemAnnouncementWatcher watcher = ref.get(); - if (watcher != null) { - if (!Files.isReadable(watcher.announcePath)) return; - - FileTime lastModifiedTime = getLastModifiedTime(watcher.announcePath); - if (lastModifiedTime.compareTo(previousFileTime) > 0) { - previousFileTime = lastModifiedTime; - - long currentVersion = watcher.readLatestVersion(); - if (watcher.latestVersion != currentVersion) { - watcher.latestVersion = currentVersion; - for (HollowConsumer consumer : watcher.subscribedConsumers) - consumer.triggerAsyncRefresh(); + // Set MDC context on the new thread + if (mdcContext != null) { + MDC.setContextMap(mdcContext); + } + try { + HollowFilesystemAnnouncementWatcher watcher = ref.get(); + if (watcher != null) { + if (!Files.isReadable(watcher.announcePath)) return; + + FileTime lastModifiedTime = getLastModifiedTime(watcher.announcePath); + if (lastModifiedTime.compareTo(previousFileTime) > 0) { + previousFileTime = lastModifiedTime; + + long currentVersion = watcher.readLatestVersion(); + if (watcher.latestVersion != currentVersion) { + watcher.latestVersion = currentVersion; + for (HollowConsumer consumer : watcher.subscribedConsumers) + consumer.triggerAsyncRefresh(); + } } } + } catch (Exception ex) { + log.log(Level.WARNING, "Exception reading the current announced version", ex); + } catch (Throwable th) { + log.log(Level.SEVERE, "Exception reading the current announced version", th); + throw th; + } finally { + MDC.clear(); } - } catch (Exception ex) { - log.log(Level.WARNING, "Exception reading the current announced version", ex); - } catch (Throwable th) { - log.log(Level.SEVERE, "Exception reading the current announced version", th); - throw th; + } finally { + MDC.clear(); } } } diff --git a/hollow/src/main/java/com/netflix/hollow/api/producer/AbstractHollowProducer.java b/hollow/src/main/java/com/netflix/hollow/api/producer/AbstractHollowProducer.java index ddb63902e3..322816a7e0 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/producer/AbstractHollowProducer.java +++ b/hollow/src/main/java/com/netflix/hollow/api/producer/AbstractHollowProducer.java @@ -47,6 +47,8 @@ import com.netflix.hollow.core.write.objectmapper.HollowObjectMapper; import com.netflix.hollow.core.write.objectmapper.RecordPrimaryKey; import com.netflix.hollow.tools.checksum.HollowChecksum; +import org.slf4j.MDC; + import java.io.IOException; import java.util.Arrays; import java.util.Collection; @@ -664,9 +666,15 @@ private void publishBlob(ProducerListeners listeners, HollowProducer.Blob blob) private void publishSnapshotBlobAsync(ProducerListeners listeners, Artifacts artifacts) { HollowProducer.Blob blob = artifacts.snapshot; CompletableFuture cf = new CompletableFuture<>(); + // Capture MDC context on the original thread + final Map mdcContext = MDC.getCopyOfContextMap(); try { snapshotPublishExecutor.execute(() -> { Status.StageBuilder builder = new Status.StageBuilder(); + // Set MDC context on the new thread + if (mdcContext != null) { + MDC.setContextMap(mdcContext); + } try { publishBlob(blob); builder.success(); @@ -682,6 +690,7 @@ private void publishSnapshotBlobAsync(ProducerListeners listeners, Artifacts art if (metricsCollector != null) { metricsCollector.collect(metrics); } + MDC.clear(); } artifacts.markSnapshotPublishComplete(); }); @@ -694,6 +703,7 @@ private void publishSnapshotBlobAsync(ProducerListeners listeners, Artifacts art throw t; } finally { listeners.fireBlobPublishAsync(cf); + MDC.clear(); } }