Skip to content

Commit

Permalink
Fix interpretation of CASSANDRA_USE_JDK11 and auto set CASSANDRA_USE_…
Browse files Browse the repository at this point in the history
…JDK11 when needed (#769)

* Fix interpretation of CASSANDRA_USE_JDK11 and auto set CASSANDRA_USE_JDK11 when needed

* Don't interpret CASSANDRA_USE_JDK11

* apply review comments

* Update ccmlib/common.py

Co-authored-by: mck <[email protected]>
  • Loading branch information
jacek-lewandowski and michaelsembwever authored May 24, 2024
1 parent d5a9a45 commit 4aae080
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
32 changes: 24 additions & 8 deletions ccmlib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,10 @@ def get_supported_jdk_versions_from_dist(install_dir):
versions = get_supported_jdk_versions_internal(os.path.join(install_dir, 'bin', 'cassandra.in.sh'),
'java_versions_supported=([0-9.,]+)')

info("Supported Java versions for Cassandra distribution in '{}': {}".format(install_dir, versions))
if versions and len(versions) > 0:
info("Supported Java versions for Cassandra distribution in '{}': {}".format(install_dir, versions))
else:
info("Supported Java versions could not be retrieved from Cassandra distribution in '{}'".format(install_dir))

return versions

Expand All @@ -899,21 +902,26 @@ def get_supported_jdk_versions(cassandra_version, install_dir, for_build, env):
if cassandra_version and not isinstance(cassandra_version, LooseVersion):
cassandra_version = LooseVersion(cassandra_version)

isCassandraUseJDK11Set = "CASSANDRA_USE_JDK11" in env and (env["CASSANDRA_USE_JDK11"].lower() in ['true', 'yes', 'on'])
if build_versions is None:
if cassandra_version and cassandra_version >= LooseVersion('5.0'):
build_versions = [11, 17]
run_versions = [11, 17]
info("Cassandra 5.0+ detected, using Java 11 and 17")
elif cassandra_version and cassandra_version >= LooseVersion('4.0'):
if 'CASSANDRA_USE_JDK11' not in env:
build_versions = [8, 11]
run_versions = [8, 11]
else:
if isCassandraUseJDK11Set:
build_versions = [11]
run_versions = [11]
info("Cassandra 4.0+ detected, using Java 11 (Java 8 is excluded because the deprecated CASSANDRA_USE_JDK11 is set)")
else:
build_versions = [8, 11]
run_versions = [8, 11]
info("Cassandra 4.0+ detected, using Java 8 and 11")
else:
# Cassandra versions 3.x and 2.x
build_versions = [8]
run_versions = [8]
info("Cassandra 3.x or 2.x detected, using Java 8")

# Java versions supported by the Cassandra distribution
return build_versions if for_build else run_versions
Expand Down Expand Up @@ -971,6 +979,14 @@ def update_path_in_env(env, path_entry):
return env


def _maybe_set_use_jdk11_env(env, jvm_version, cassandra_version):
if jvm_version >= 11 and cassandra_version and '4.0' <= cassandra_version < '5.0':
env['CASSANDRA_USE_JDK11'] = 'true'
elif 'CASSANDRA_USE_JDK11' in env:
del env['CASSANDRA_USE_JDK11']
return env


def _update_java_version(current_java_version, current_java_home_version,
jvm_version=None, install_dir=None, cassandra_version=None, env=None,
for_build=False, info_message=None, os_env=None):
Expand Down Expand Up @@ -1004,13 +1020,13 @@ def _update_java_version(current_java_version, current_java_home_version,
info('{}: Using the current Java {} available on PATH for the current invocation of Cassandra {}.'
.format(info_message, current_java_version, cassandra_version))
# nothing to change in this case
return env
return _maybe_set_use_jdk11_env(env, current_java_version, cassandra_version)

elif current_java_home_version and current_java_home_version in supported_versions:
info('{}: Using the current Java {} available from JAVA_HOME for the current invocation of Cassandra {}.'
.format(info_message, current_java_version, cassandra_version))
# just need to add JAVA_HOME/bin to the PATH
return update_path_in_env(env, env['JAVA_HOME'] + '/bin')
return _maybe_set_use_jdk11_env(update_path_in_env(env, env['JAVA_HOME'] + '/bin'), current_java_home_version, cassandra_version)

elif current_java_home_version and current_java_home_version not in supported_versions:
warning('{}: The current Java {} is not supported by Cassandra {} (supported versions: {}).'
Expand Down Expand Up @@ -1042,7 +1058,7 @@ def _update_java_version(current_java_version, current_java_home_version,
.format(info_message, jvm_version, cassandra_version))

env['JAVA_HOME'] = env[available_versions[jvm_version]]
return update_path_in_env(env, env['JAVA_HOME'] + '/bin')
return _maybe_set_use_jdk11_env(update_path_in_env(env, env['JAVA_HOME'] + '/bin'), jvm_version, cassandra_version)


def assert_jdk_valid_for_cassandra_version(cassandra_version, env=None):
Expand Down
13 changes: 11 additions & 2 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ def test_supported_jdk_versions(self):
self.assertIn(8, get_supported_jdk_versions(cassandra_version, None, False, {'key': 'value'}))
self.assertIn(8, get_supported_jdk_versions(cassandra_version, None, True, {'key': 'value'}))

for cassandra_version in [None, '2.2', '3.0', '3.1', '4.0', '4.1']:
self.assertIn(8, get_supported_jdk_versions(cassandra_version, None, False, {'key': 'value', 'CASSANDRA_USE_JDK11': 'false'}))
self.assertIn(8, get_supported_jdk_versions(cassandra_version, None, True, {'key': 'value', 'CASSANDRA_USE_JDK11': 'false'}))

for cassandra_version in ['4.0', '4.1', '5.0', '5.1']:
self.assertNotIn(8, get_supported_jdk_versions(cassandra_version, None, False, {'CASSANDRA_USE_JDK11': 'true'}))
self.assertNotIn(8, get_supported_jdk_versions(cassandra_version, None, True, {'CASSANDRA_USE_JDK11': 'true'}))
for usd_jdk_11 in ['true', 'TRUE', 'True', 'on', 'ON', 'On', 'yes', 'YES', 'Yes']:
self.assertNotIn(8, get_supported_jdk_versions(cassandra_version, None, False, {'CASSANDRA_USE_JDK11': usd_jdk_11}))
self.assertNotIn(8, get_supported_jdk_versions(cassandra_version, None, True, {'CASSANDRA_USE_JDK11': usd_jdk_11}))

for cassandra_version in ['4.0', '4.1', '5.0', '5.1']:
self.assertIn(11, get_supported_jdk_versions(cassandra_version, None, False, {'key': 'value'}))
Expand Down Expand Up @@ -144,6 +149,10 @@ def _test_java_selection(self, expected_version, path_version, home_version, exp
env=self._make_env(java_home_version=home_version, java_path_version=path_version, include_homes=available_homes),
for_build=True, info_message='test_java_selection_{}'.format(cassandra_version), os_env={'key': 'value'})
self._check_env(result_env, expected_version)
if expected_version >= 11 and '4.0' <= cassandra_version < '5.0':
self.assertEqual(result_env['CASSANDRA_USE_JDK11'], 'true')
else:
self.assertNotIn('CASSANDRA_USE_JDK11', result_env)

def _test_java_selection_fail(self, expected_failure_regexp, path_version, home_version, explicit_version, cassandra_versions, available_homes=None):
for cassandra_version in cassandra_versions:
Expand Down

0 comments on commit 4aae080

Please sign in to comment.