Skip to content

Step Functions: Lazy Initialization of JVM for JSONata Evaluation #12369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025

Conversation

MEPalma
Copy link
Contributor

@MEPalma MEPalma commented Mar 11, 2025

Motivation

The current implementation starts the JVM during module import, which interferes with unrelated processes like generating entrypoints—especially on Windows machines, where it may break the entrypoint generation process entirely. This PR defers the startup of the JVM until the JSONata evaluation feature is actually needed first, ensuring that other processes remain unaffected by premature JVM initialization.

Changes

  • Delay JVM instantiation until the first JSONata evaluation call, then cache the function reference to ensure fast, persistent access throughout the application’s runtime.

* Known Limitation: The first invocation in a state with tight timeouts might trigger a timeout error in the evaluation of the state machine due to the JVM installation overhead. This edge case is rare, does not result in abrupt terminations (the state machine will terminate gracefully), and does not affect future executions. Future refinements to the installer behavior may address this further.

@MEPalma MEPalma added the semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases label Mar 11, 2025
@MEPalma MEPalma added this to the 4.3 milestone Mar 11, 2025
@MEPalma MEPalma requested a review from alexrashed March 11, 2025 20:34
@MEPalma MEPalma self-assigned this Mar 11, 2025
Copy link

github-actions bot commented Mar 11, 2025

LocalStack Community integration with Pro

    2 files  ±    0      2 suites  ±0   31m 14s ⏱️ - 1h 21m 15s
1 452 tests  - 2 692  1 379 ✅  - 2 445  73 💤  - 247  0 ❌ ±0 
1 454 runs   - 2 692  1 379 ✅  - 2 445  75 💤  - 247  0 ❌ ±0 

Results for commit cd9cc09. ± Comparison against base commit 20f989b.

This pull request removes 2692 tests.
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_lambda_dynamodb
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_opensearch_crud
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_search_books
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_setup
tests.aws.scenario.kinesis_firehose.test_kinesis_firehose.TestKinesisFirehoseScenario ‑ test_kinesis_firehose_s3
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_destination_sns
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_infra
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_prefill_dynamodb_table
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input0-SUCCEEDED]
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input1-SUCCEEDED]
…

♻️ This comment has been updated with latest results.

@alexrashed
Copy link
Member

I'll let @joe4dev and @gregfurman do the actual review, but I can verify that this solves the issue with the entrypoint generation.
make entrypoints | grep JVM fails on this PR (as no log with "JVM" is written) while there are lots of errors logged on the current master.

Copy link
Contributor

@gregfurman gregfurman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one small concern about the global variable assign being racey.

Comment on lines +95 to +98
global _eval_jsonata
if _eval_jsonata is None:
# Initialize _eval_jsonata only when invoked for the first time using the Singleton pattern.
_eval_jsonata = _JSONataJVMBridge.get().eval_jsonata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not be locking before assigning this global variable? I'm concerned about a race condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The singleton class pattern and the installer are thread-safe, and the installation can only occur once, so we should be okay to keep this as it

@MEPalma MEPalma merged commit 30940d8 into master Mar 17, 2025
31 checks passed
@MEPalma MEPalma deleted the MEP-SFN-lazy_JSONata_JVM_start branch March 17, 2025 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants