Repository navigation
[Feature][Client] Add Java detection utility to Python SDK - #12740
Conversation
DanielLeens
left a comment
There was a problem hiding this comment.
Thanks for the contribution, and for the clear PR description and design note! I went through the full diff and traced the logic path by path. I did not find any blocking issue in the code. Details below.
What Problem Does This PR Solve?
- User pain point: Today there is no Java version check before launching SeaTunnel, so a user on an older JDK gets an opaque
UnsupportedClassVersionError. The Java 11 baseline matchesjava.version=11in the rootpom.xmland the "Java 11 or Java 17" requirement indocs/en/getting-started/locally/deployment.md. - Fix approach: Add
detect_java(min_version=11)andJavaInfo/JavaDetectionErrorintools/seatunnel-python-sdk/seatunnel/java_detector.py, as a first building block for #12707. - One-sentence summary: A small, self-contained, currently-unused helper that resolves Java (
JAVA_HOMEfirst, thenPATH), parses its major version, and fails early with an actionable message.
1. Code Change Review
1.1 Core Logic Analysis
Runtime path: detect_java -> _find_java_executable (JAVA_HOME/bin/java[.exe], or shutil.which("java") only when JAVA_HOME is unset/blank) -> subprocess.run([exe, "-version"], timeout=10) -> _parse_major_version on stderr, then stdout -> compare with min_version -> JavaInfo.
What I verified:
- Version parsing handles
1.7.0_80/1.8.0_292(-> 7 / 8),11.0.2,17,17-ea,21.0.1correctly. Thefirst == "1" and second is not Nonebranch is correct, and a bare"17"correctly falls to the new-style path. Picked up JAVA_TOOL_OPTIONS ...noise lines are skipped, andorbetween the two parses is safe because a real major version is never 0.- Failure modes are all mapped to
JavaDetectionErrorwithfrom excchaining: timeout,OSError, non-zero exit, unparseable output, too old. - The "bad
JAVA_HOMEdoes not silently fall back toPATH" decision is reasonable for a launcher, and I agree with it. - Python 3.9 compatible (
Optional,dataclass(frozen=True), f-strings), matchingpython_requires='>=3.9'. - New files carry the ASF license header.
1.2 Compatibility Impact
Fully compatible. Two new files only; not exported from seatunnel/__init__.py, nothing calls the helper, seatunnel.sh and the engine are untouched. No API, config, default, protocol or serialization impact.
1.3 Performance / Side-Effect Analysis
One short-lived subprocess with a 10s timeout; no shared state, no retries, no resources left open. No concerns.
1.4 Error Handling and Logging
Messages include what was found and a next step, which is exactly what the launcher needs. Nothing is swallowed and no sensitive data is printed (only paths and java -version output).
No blocking issues found. Non-blocking suggestions:
Issue 1: JAVA_HOME candidate is only checked with isfile
- Location:
tools/seatunnel-python-sdk/seatunnel/java_detector.py(_find_java_executable, theos.path.isfile(candidate)check) - Problem description: A non-executable file at
JAVA_HOME/bin/javapasses the check and then fails insubprocess.runwithPermissionError. That is already converted toJavaDetectionErrorvia theOSErrorbranch, so the user still gets a message, just a slightly less direct one. - Potential risk: Cosmetic only.
- Best improvement: Optionally use
os.path.isfile(candidate) and os.access(candidate, os.X_OK). - Severity: Low
- Raised by another reviewer: No
Issue 2: The min_version default is duplicated
- Location:
java_detector.py(_find_java_executableanddetect_javaboth default to11) - Problem description: The baseline lives in two signatures; they can drift when the baseline moves (for example to 17).
- Best improvement: A module constant such as
DEFAULT_MIN_JAVA_VERSION = 11, or make the private helper's parameter required. - Severity: Low
- Raised by another reviewer: No
2. Code Quality Assessment
2.1 Coding Standards
Clean, well-named, and the docstrings explain resolution order and constraints. Nothing missing on the documentation front.
2.2 Test Coverage and Test Stability
The 20 tests mock subprocess.run, shutil.which and the environment, and cover old/new version formats, JAVA_HOME precedence, bad JAVA_HOME, missing/too-old/unreadable/non-zero/timeout/OSError, JVM noise lines, stdout fallback, and a custom min_version. Platform differences are handled with sys.platform in the expected path.
- Stability rating: Stable. No real JDK, network, sleeps, or ordering dependence; the environment is isolated via
mock.patch.dict(..., clear=True). - Small note (no action required): I could not find a CI job that runs the Python SDK tests, so these tests are currently only exercised when run locally with
python -m pytest tests/ -v. Worth keeping in mind for the follow-up launcher PRs.
2.3 Documentation Updates
Not needed: the helper is internal and not user-facing yet. docs/en / docs/zh need no change in this PR.
3. Architectural Soundness
3.1 Elegance of the Solution
Precise, minimal first step toward #12707.
3.2 Maintainability
Small and readable; error text is centralized in the one function.
3.3 Extensibility
Returning JavaInfo(executable, major_version) lets the launcher reuse both values, which is a good shape.
3.4 Historical-Version Compatibility
Fully compatible; no upgrade action.
4. Issue Summary
| # | Issue | Location | Severity |
|---|---|---|---|
| 1 | JAVA_HOME candidate checked only with isfile |
java_detector.py _find_java_executable |
Low |
| 2 | Default min_version duplicated |
java_detector.py |
Low |
5. Merge Recommendation
Conclusion: Ready to merge
- Blockers - must be fixed: none on the code side.
- Recommended fixes - non-blocking: Issue 1 and Issue 2 (both Low, purely optional).
CI status (current head 8e75f8c): the apache-side Build check is only a pointer; the real run is in your fork. The failures there are the Java integration jobs (all-connectors-it-2 on Java 11 and 17, and engine-v2-it (11)), while all unit-test, code-style, license-header and docs jobs are green. This PR only adds two Python files that no Java code path touches, so those failures do not look related to this change. The branch is also 3 commits behind dev (diverged), so the lowest-cost next step is to sync with the latest dev and re-run the failed jobs. If the same jobs fail again after that, please paste the new failing link here and I am happy to help narrow it down.
Overall: a nice, well-tested starting point. Thanks again, and happy to take another look after the sync!
8e75f8c to
6acc237
Compare
|
Pushed an update, rebased on the latest dev. Thanks @DanielLeens for the review.
Tests: 28 passed on Python 3.9 and 3.13. |
|
Rechecked All 21 detector tests pass locally on Python 3.13. I also exercised this head with real Java 8/11/17 on macOS: 11 and 17 resolve correctly; 8 is rejected with its JAVA_HOME value, found version and unset guidance. A missing JAVA_HOME is rejected with the attempted value and recovery hint. This covers the detection helper; I have not tested Windows execution or the complete pip launcher. |
DanielLeens
left a comment
There was a problem hiding this comment.
Thanks @karthik-0306 for the update and for the clear explanation of what you took and what you left alone! I re-read the full diff of the new head 6acc237f (2 commits on top of current dev, ahead_by=2, behind_by=0, so the sync is done), re-traced detect_java end to end, and read the issue #12707 thread plus @SEZ9's two asks and @CryoThrust's real-JDK check. Short version: my earlier Issue 2 is fixed, I accept your reasoning on Issue 1, and I found no blocker.
What Problem Does This PR Solve?
- User pain point: There is no Java version check before launching SeaTunnel, so a user on an older JDK gets an opaque
UnsupportedClassVersionError. The Java 11 baseline matches the rootpom.xml(java.version=11). - Fix approach: Add
detect_java(min_version=DEFAULT_MIN_JAVA_VERSION),JavaInfoandJavaDetectionErrorintools/seatunnel-python-sdk/seatunnel/java_detector.py, as the first building block for #12707. - One-sentence summary: A small, self-contained, currently unused helper that resolves Java (
JAVA_HOMEfirst, thenPATH), parses its major version, and fails early with an actionable message.
1. Code Change Review
1.1 Core Logic Analysis
Runtime path:
detect_java(min_version)
-> _find_java_executable: JAVA_HOME set -> JAVA_HOME/bin/java[.exe] or JavaDetectionError (no fallback to PATH)
JAVA_HOME unset/blank -> shutil.which("java")
-> subprocess.run([exe, "-version"], timeout=10) (TimeoutExpired / OSError -> JavaDetectionError, chained)
-> non-zero exit -> JavaDetectionError
-> _parse_major_version(stderr) or (stdout) ("1.8.0_292" -> 8, "11.0.2" -> 11, "17-ea" -> 17)
-> major < min_version -> JavaDetectionError (mentions JAVA_HOME value when the binary came from it)
-> JavaInfo(executable, major)
Status of my previous round and the new asks:
- Earlier Issue 2 (duplicated default): fixed with
DEFAULT_MIN_JAVA_VERSION(java_detector.py:45), used by both signatures. - Earlier Issue 1 (
isfileonly): I accept your reasoning. A non-executableJAVA_HOME/bin/javafails insubprocess.runwithPermissionError, which theOSErrorbranch already converts intoJavaDetectionErrorwith the path, so the user still gets a clear message. No change needed. - @SEZ9's two asks (error shows the tried
JAVA_HOMEvalue and why it was rejected, and a test): both covered. "Nothing found there" is in_find_java_executable(:61-64) and "version below minimum" is indetect_java(:144-155, includes theJAVA_HOMEvalue, the resolved executable, the found version and "Fix or unset JAVA_HOME"). The tests aretest_java_home_set_but_binary_missing_raisesandtest_java_home_too_old_error_mentions_java_home_value. - Version parsing re-checked:
1.8.0_292/1.7.0_80-> 8/7, bare17,17-ea,21.0.1all land on the right branch (java_detector.py:75-86). Python 3.9 compatible (Optional,dataclass(frozen=True)), matchingpython_requires='>=3.9'insetup.py.
1.2 Compatibility Impact
Fully compatible. Two new files only; nothing imports the helper, seatunnel/__init__.py, seatunnel.sh and the engine are untouched. No API, config, default, protocol or serialization impact, and no incompatible-changes.md entry needed.
1.3 Performance / Side-Effect Analysis
One short-lived subprocess with a 10s timeout, no shared state, no retries, nothing left open.
1.4 Error Handling and Logging
Every failure mode maps to JavaDetectionError with from exc chaining where relevant; nothing is swallowed and no sensitive data is printed.
Issue 1 (Low, non-blocking): errors after the JAVA_HOME binary is resolved do not follow the same "fix or unset JAVA_HOME" guidance
- Location:
java_detector.py:108-139(timeout,OSError, non-zero exit, unparseable output branches) - Problem description: When
JAVA_HOMEis set and its binary exists but cannot be run, exits non-zero or prints something unparseable, the message ends with "Set JAVA_HOME or install JDK N+." SinceJAVA_HOMEis already set, that hint is slightly misleading, and the message does not say the executable came fromJAVA_HOME. The executable path is shown, so the user can still figure it out. @SEZ9's ask covered "nothing found" and "version below minimum", which are handled, so this is only a consistency polish. - Potential risk: Cosmetic; a less direct message for a rare broken-JDK case.
- Best improvement: Reuse the "from JAVA_HOME='...' ... Fix or unset JAVA_HOME" wording through a small helper used by all branches, or leave as is for this first PR and handle it in the launcher PR.
- Severity: Low
- Raised by another reviewer: No
2. Code Quality Assessment
2.1 Coding Standards
Clean and well-named, docstrings explain resolution order and constraints, ASF headers are present on both new files.
2.2 Test Coverage and Test Stability
test_java_detector.py has 21 mocked tests covering old and new version formats, JAVA_HOME precedence, bad JAVA_HOME (no fallback to PATH, enforced with a shutil.which mock that raises), too-old JAVA_HOME, missing java, timeout, OSError, non-zero exit, unparseable output, stdout fallback, JVM option noise lines, and custom min_version in all error messages. Thanks also to @CryoThrust for running it against real Java 8/11/17 on macOS (11 and 17 resolve; 8 is rejected with the JAVA_HOME value and unset hint); I did not run it myself.
Stability rating: Stable. No real JDK, network, sleeps or ordering dependence; the environment is isolated with mock.patch.dict("os.environ", ..., clear=True) (test_java_detector.py:51-56), and the platform difference is handled with sys.platform.
Issue 2 (Low, non-blocking): the Picked up guard does not match the NOTE: Picked up ... form, and its test passes for a different reason
- Location:
java_detector.py:76(line.strip().startswith("Picked up")) andtest_java_detector.py:230(test_note_picked_up_jdk_java_options_line_is_ignored) - Problem description: The JDK prints
NOTE: Picked up JDK_JAVA_OPTIONS: ...(with theNOTE:prefix), which does not start withPicked up, so the guard does not skip it. The test still passes only because the regex needsversion "with a space before the quote and the sample option-Dfoo="1"does not contain that. So the test docstring ("is skipped") overstates what protects it, and an option such as-Dx="a version "9""is an unrealistic but possible way to fool the parser. - Potential risk: Very low; the regex already ignores realistic option strings.
- Best improvement: Use
"Picked up" in linefor the skip, or reword the docstring to say the line simply does not match the version regex. - Severity: Low
- Raised by another reviewer: No
Note, no action required: I could not find a CI job that runs tools/seatunnel-python-sdk tests (.github/workflows/cli.yml covers seatunnel-cli/ only), so these tests run only locally with python -m pytest tests -v. Worth keeping in mind for the launcher PRs.
2.3 Documentation Updates
Not needed: the helper is internal and not user-facing yet, so docs/en and docs/zh need no change.
3. Architectural Soundness
3.1 Elegance of the Solution
Precise, minimal first step toward #12707.
3.2 Maintainability
Small and readable; the baseline now lives in one constant.
3.3 Extensibility
Returning JavaInfo(executable, major_version) lets the launcher reuse both values.
3.4 Historical-Version Compatibility
Fully compatible; no upgrade action.
4. Issue Summary
| # | Issue | Location | Severity |
|---|---|---|---|
| 1 | Post-resolution errors do not use the "fix or unset JAVA_HOME" wording | java_detector.py:108-139 |
Low |
| 2 | Picked up guard misses the NOTE: form; test passes for another reason |
java_detector.py:76, test_java_detector.py:230 |
Low |
5. Merge Recommendation
Conclusion: Ready to merge
- Blockers - must be fixed: none on the code, test, docs or compatibility side.
- Recommended fixes - non-blocking: Issues 1 and 2 (both Low, optional, fine for a follow-up).
Merge state and CI: the branch is up to date with dev (ahead 2, behind 0) with no conflicts. At the time of this review the apache-side checks for this head show only Notify test workflow and labeler as queued, and there is no Build check yet, so there is no CI result to evaluate. The previous head's red jobs were Java integration lanes unrelated to these two Python files. I would hold the merge itself until Build has run and is green, and a committer approval is still needed on top of this technical review. @SEZ9 has also said this PR is on their review list.
Overall: a nice, well-tested starting point. Thanks again!
|
@SEZ9 the JAVA_HOME error changes and the new test are pushed to this PR, so the latest head already includes them. I ran the full suite locally on Windows with Python 3.9 and 3.13: 28 passed (21 detector tests including the new JAVA_HOME too-old test, plus the 7 existing SDK tests). I haven't run it on macOS or Linux myself beyond CryoThrust's run. Thanks for the review. |
|
Thanks @karthik-0306 for the clear follow-up, and thanks @SEZ9 and @CryoThrust for the extra verification. Nothing further is needed from the code side for my review. One CI correction on my side: in my last review I wrote that the apache-side |
|
@karthik-0306 thanks for the follow-up — confirmed, Also agree with leaving the Two small things left before merge:
Once |
|
Thanks @SEZ9 for the question, and thanks @karthik-0306 for the quick turnaround. I checked the CI wiring on current On the second point (do the new detector tests run in CI): as far as I can tell from the workflow files, no. The only Python workflow is My suggestion is to keep that out of this PR: wiring a path-filtered pytest job for the SDK is a separate CI change that also affects the existing SDK tests, and the detector has already been verified locally on Windows and macOS (thanks @CryoThrust). It would fit well together with the launcher follow-up for #12707, or as a small dedicated PR. On the first point ( |
DanielLeens
left a comment
There was a problem hiding this comment.
CI update for head 6acc237f0, with no code change since my last review.
In my last review there was no Build check on this head yet, and I said I would hold the merge until it ran and was green. That fact has changed. The apache-side Build check (see https://github.com/apache/seatunnel/runs/116944660568) is now SUCCESS on this exact head.
CI was previously the only remaining blocker, and it is now passing, so I am changing my conclusion to approve. I did not find any code-side blocker in my last review and nothing has changed in the diff since.
This approval is based on a static review of the code plus the passing CI above; I did not execute the PR myself. A committer approval is still required for merge.
Thanks for the contribution!
davidzollo
left a comment
There was a problem hiding this comment.
Approving: Build is green on this exact head and the code-side review found no blockers.
Purpose of this pull request
Related to #12707 (pip-installable SeaTunnel launcher).
This PR adds the first small building block for that work: a helper that finds Java on the user's machine and checks that it is new enough. Today
seatunnel.shhas no Java version check, so someone running Java 8 gets a confusingUnsupportedClassVersionErrorfrom the JVM. This helper lets the future launcher fail early with a clear message instead.What
detect_java(min_version=11)does:$JAVA_HOME/bin/javafirst, then falls back tojavaonPATH.java -versionand reads the major version. It handles the old format (1.8.0_292is Java 8) and the new formats (11.0.2,17,21.0.1,17-ea).JavaInfo(executable, major_version)object, so the launcher can reuse both the path and the version without parsing anything again.JavaDetectionErrorif Java is missing, older thanmin_version, its output can't be read, or running it fails or times out. Every message states what was found (or that nothing was found) and guides the user to setJAVA_HOMEor installJDK {min_version}+(default 11+).Design note: If
JAVA_HOMEis set but has no Java binary in it, the helper raises an error instead of quietly falling back toPATH. A typo inJAVA_HOMEwould otherwise be ignored, and the user could end up on a different Java than intended.PATHis only consulted whenJAVA_HOMEis unset or empty. Happy to adapt this if reviewers prefer a fallback.Scope: This adds two new files under
tools/seatunnel-python-sdk. It is not exported fromseatunnel/__init__.py, andseatunnel.shand the engine are untouched. Adding the same check toseatunnel.shcould be handled in a separate change.Does this PR introduce any user-facing change?
No. It is an internal helper that nothing calls yet, and it is not part of the SDK's public API.
How was this patch tested?
Added 20 new unit tests in
tools/seatunnel-python-sdk/tests/test_java_detector.py. They mock thejava -versionoutput, so no real JDK is needed. They cover:1.7.0_80,1.8.0_292,11,11.0.2,17.0.1,17-ea,21.0.1JAVA_HOMEused beforePATH, and a badJAVA_HOMEfailing without falling back toPATHOSErrorPicked up JAVA_TOOL_OPTIONSorNOTE: Picked up JDK_JAVA_OPTIONS, and a version printed on stdout instead of stderrmin_version, with all error messages using that value instead of a hardcoded 11To run them (from
tools/seatunnel-python-sdk/):