gpt4 book ai didi

multithreading - 子线程看不到主线程所做的更新

转载 作者:行者123 更新时间:2023-12-03 23:35:41 24 4
gpt4 key购买 nike

我正在通过扩展 SparkListener 类来实现 SparkHealthListener。

@Component
class ClusterHealthListener extends SparkListener with Logging {
val appRunning = new AtomicBoolean(false)
val executorCount = new AtomicInteger(0)

override def onApplicationStart(applicationStart: SparkListenerApplicationStart) = {
logger.info("Application Start called .. ")
this.appRunning.set(true)
logger.info(s"[appRunning = ${appRunning.get}]")
}

override def onExecutorAdded(executorAdded: SparkListenerExecutorAdded) = {
logger.info("Executor add called .. ")
this.executorCount.incrementAndGet()
logger.info(s"[executorCount = ${executorCount.get}]")
}
}

应用程序运行 executorCount 是在 ClusterHealthListener 类中声明的两个变量。 ClusterHealthReporterThread 只会读取这些值。
@Component
class ClusterHealthReporterThread @Autowired() (healthListener: ClusterHealthListener) extends Logging {
new Thread {
override def run(): Unit = {
while (true) {
Thread.sleep(10 * 1000)
logger.info("Checking range health")
logger.info(s"[appRunning = ${healthListener.appRunning.get}] [executorCount=${healthListener.executorCount.get}]"
}
}
}.start()
}

无论主线程对变量所做的更改如何,ClusterHealthReporterThread 总是报告初始化值?我究竟做错了什么?这是因为我向 ClusterHealthReporterThread 注入(inject)了 healthListener 吗?

更新

我玩了一下,看起来这与我启动 spark 监听器的方式有关。

如果我像这样添加 Spark 监听器
val sparkContext = SparkContext.getOrCreate(sparkConf)
sparkContext.addSparkListener(healthListener)

父线程将显示 应用程序运行 始终为“假”,但正确显示执行者计数。子线程(健康报告者)也将显示正确的执行者计数,但 应用程序运行 总是像主线程一样报告“错误”。

然后我偶然发现了这个 Why is SparkListenerApplicationStart never fired?并尝试在 Spark 配置级别设置监听器,
.set("spark.extraListeners", "HealthListener class path")
如果我这样做,主线程将为 报告“真”应用程序运行 并将报告 正确的执行者计数 但子线程将始终为执行者报告“假”和“0”值。

最佳答案

我无法立即看出这里出了什么问题,您可能已经找到了一个有趣的边缘案例。

我认为@m4gic 的评论可能是正确的,日志库可能正在缓存该插值字符串?看起来您正在使用 https://github.com/lightbend/scala-logging它声称这种插值“对行为没有影响”,所以可能不是。请您按照他的建议在不使用该功能的情况下重试并报告吗?

第二种可能:不知道只有一个ClusterHealthListener在系统中?也许 Autowiring 导致创建第二个实例?你能登录object ids吗?的ClusterHealthListener在这两个位置引用并验证它们是否是同一个对象?

如果这些建议都不能解决这个问题,你能发布一个我可以玩的工作示例吗?

关于multithreading - 子线程看不到主线程所做的更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52323355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com