Skip to content
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

Check if DH Enterprise Python API is installed by mistake and if so produce a helpful message #3673

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ private static void init(JpyConfigExt jpyConfig) {
jpyConfig.initPython();
jpyConfig.startPython();
log.info().append("Started Python interpreter").endl();
markJvmReady();
markReadyAndCheckEnv();
}

private static void markJvmReady() {
private static void markReadyAndCheckEnv() {
// noinspection EmptyTryBlock,unused
try (
final PyModule deephavenJpyModule = PyModule.importModule("deephaven_internal.jvm");
final PyObject obj = deephavenJpyModule.callMethod("ready")) {
final PyObject readyObj = deephavenJpyModule.callMethod("ready");
final PyObject checkObj = deephavenJpyModule.callMethod("check_py_env")) {
// empty
}
}

}
2 changes: 2 additions & 0 deletions py/embedded-server/deephaven_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
from .start_jvm import DEFAULT_JVM_PROPERTIES, DEFAULT_JVM_ARGS, start_jvm
from .server import Server

from deephaven_internal.jvm import check_py_env
check_py_env()
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 36 additions & 4 deletions py/server/deephaven_internal/jvm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
_is_ready = False


def ready():
"""Marks the JVM as ready. Should be called by Deephaven implementation code. In the case of the Java server process,
this should be called right after Python has been initialized. In the case of the embedded Python server process,
this should be called right after the JVM has been initialized."""
global _is_ready
_is_ready = True


def check_ready():
"""Checks if the JVM is ready (ie, if ready() has been called). Should be called by Deephaven implementation code.
Raises an exception if the JVM is not ready."""
Raises a RuntimeError if the JVM is not ready.

Raises:
RuntimeError
"""
# Note: we might be tempted to store the source of truth for this in Java, but we aren't able to do that.
# `import jpy` has potential side effects, and may improperly start the JVM.
global _is_ready
if not _is_ready:
raise Exception("The Deephaven Server has not been initialized. "
raise RuntimeError("The Deephaven Server has not been initialized. "
"Please ensure that deephaven_server.Server has been constructed before importing deephaven.")


def check_py_env():
"""Checks if the current Python environment is in good order and if not, raises a RuntimeError.

Raises:
RuntimeError
"""
import importlib.metadata
try:
importlib.metadata.version("deephaven")
except:
pass
else:
jmao-denver marked this conversation as resolved.
Show resolved Hide resolved
# DH Enterprise deephaven package is installed by mistake
raise RuntimeError("The Deephaven Enterprise Python Package (name 'deephaven' on pypi) is installed in "
"the current Python environment. It conflicts with the Deephaven Community Python "
"Package (name 'deephaven-core' on pypi). Please uninstall the 'deephaven' package and "
"reinstall the 'deephaven-core' package.")


def preload_jvm_dll(*args, **kwargs):
"""A wrapper around jpyutil.preload_jvm_dll(...)."""
import jpyutil
result = jpyutil.preload_jvm_dll(*args, **kwargs)
return result


def init_jvm(*args, **kwargs):
"""A wrapper around jpyutil.init_jvm(...). Should be called by Deephaven implementation code.
Calls ready() after jpyutil.init_jvm(...)."""
Calls ready() after jpyutil.init_jvm(...).

Raises:
ImportError
"""
# Note: we might be able to use our own logic instead of jpyutil here in the future
import jpyutil
try:
result = jpyutil.init_jvm(*args, **kwargs)
except ImportError as e:
raise ImportError("Unable to initialize JVM, try setting the environment variable JAVA_HOME (JDK 11+ required)") from e
raise ImportError(
"Unable to initialize JVM, try setting the environment variable JAVA_HOME (JDK 11+ required)") from e
ready()
return result