You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "waterWang (via GitHub)" <gi...@apache.org> on 2026/08/02 10:22:39 UTC

[PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

waterWang opened a new pull request, #19856:
URL: https://github.com/apache/druid/pull/19856

   Fixes #18253
   
   ## Problem
   GraalVM JS engine throws `IllegalStateException: Multi threaded access requested` when the same JavaScript context is accessed from multiple threads simultaneously. The `JavaScriptTieredBrokerSelectorStrategy` shares a single compiled JavaScript function across all broker routing threads, which triggers this error.
   
   ## Root cause
   `JavaScriptUtil.compileSelectorFunction()` creates a GraalVM `ScriptEngine` that is not thread-safe. The `fnSelector` field is lazily initialized with a non-atomic check-then-write pattern, and `fnSelector.apply()` is called from multiple Jetty handler threads without synchronization.
   
   ## Fix
   1. Add `synchronized` to `getBrokerServiceName()` to serialize all access to the shared GraalVM context
   2. Simplify the lazy initialization to a straightforward if-null check (protected by the synchronized block)
   
   ## Testing
   - Existing tests pass as-is (the test creates a single-threaded test, which is unaffected)
   - The fix prevents the `Multi threaded access requested` error in production Router deployments
   
   CC: @jtuglu1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on PR #19856:
URL: https://github.com/apache/druid/pull/19856#issuecomment-5159560813

   I think we should honestly remove JS support for the next release – wdyt @kfaraz @clintropolis @gianm? The JS functionality has long been source of bugs/CVEs, etc.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #19856:
URL: https://github.com/apache/druid/pull/19856#issuecomment-6512298118

   This pull request/issue has been closed due to lack of activity. If you think that
   is incorrect, or the pull request requires review, you can revive the PR at any time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #19856: fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT]
URL: https://github.com/apache/druid/pull/19856


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "FrankChen021 (via GitHub)" <gi...@apache.org>.
FrankChen021 commented on code in PR #19856:
URL: https://github.com/apache/druid/pull/19856#discussion_r3699120351


##########
services/src/main/java/org/apache/druid/server/router/JavaScriptTieredBrokerSelectorStrategy.java:
##########
@@ -53,9 +53,11 @@ public JavaScriptTieredBrokerSelectorStrategy(
   }
 
   @Override
-  public Optional<String> getBrokerServiceName(TieredBrokerConfig config, Query query)
+  public synchronized Optional<String> getBrokerServiceName(TieredBrokerConfig config, Query query)

Review Comment:
   Although Codex reports 'LGTM', I have different opinons.
   
   If you look at the usage of compileSelectorFunction, there's another place that uses it, and the 'synchronized' is not added. So adding 'synchronized' to caller is not good, we may forget to add it in new places. 
   
   As this is due to script engine layer problem, I think it's the `compileSelectorFunction` that ensures that the returned fnSelector is a concurrent safe version.
   
   One way is that we can use dynamic proxy inside compileSelectorFunction to wrap existing object and forward the call in a synchronized block
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "FrankChen021 (via GitHub)" <gi...@apache.org>.
FrankChen021 commented on code in PR #19856:
URL: https://github.com/apache/druid/pull/19856#discussion_r3699120351


##########
services/src/main/java/org/apache/druid/server/router/JavaScriptTieredBrokerSelectorStrategy.java:
##########
@@ -53,9 +53,11 @@ public JavaScriptTieredBrokerSelectorStrategy(
   }
 
   @Override
-  public Optional<String> getBrokerServiceName(TieredBrokerConfig config, Query query)
+  public synchronized Optional<String> getBrokerServiceName(TieredBrokerConfig config, Query query)

Review Comment:
   Although Codex reports 'LGTM', I have different opinons.
   
   If you look at the usage of compileSelectorFunction, there's another place that uses it, and the 'synchronized' is not added. So adding 'synchronized' to caller is not good, we may forget to add it in new places. 
   
   As this is due to script engine layer problem, I think it's the `compileSelectorFunction` that ensure the returned fnSelector is concurrent safe version.
   
   One way is that we can use dynamic proxy inside compileSelectorFunction to wrap existing object and forward the call in a synchronized block
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] fix: synchronize GraalVM JavaScript context access in JavaScriptTieredBrokerSelectorStrategy (#18253) [fj4WqyCCw3C5ShR1RfB7MoBPTpkRrBFYP1uT35g3MvT] (druid)

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #19856:
URL: https://github.com/apache/druid/pull/19856#issuecomment-5943385428

   This pull request has been marked as stale due to 60 days of inactivity.
   It will be closed in 4 weeks if no further activity occurs. If you think
   that's incorrect or this pull request should instead be reviewed, please simply
   write any comment. Even if closed, you can still revive the PR at any time or
   discuss it on the dev@druid.apache.org list.
   Thank you for your contributions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org