Skip to content

Commit da71f55

Browse files
authored
PHOENIX-7143 Detect JVM version and add the necessary module flags in startup scripts (apache#142)
1 parent 636b4bf commit da71f55

File tree

3 files changed

+100
-31
lines changed

3 files changed

+100
-31
lines changed

Diff for: bin/phoenix_queryserver_utils.py

+81
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import os
2323
import fnmatch
24+
import re
2425
import subprocess
2526

2627
def find(pattern, classPaths):
@@ -172,6 +173,81 @@ def setPath():
172173
logging_jar += ":"+findFileInPathWithoutRecursion(LOGGING_JAR_PATTERN2, os.path.join(current_dir, "..","lib"))
173174
logging_jar += ":"+findFileInPathWithoutRecursion(LOGGING_JAR_PATTERN3, os.path.join(current_dir, "..","lib"))
174175

176+
__set_java_home()
177+
__set_jvm_flags()
178+
return ""
179+
180+
181+
def __set_java_home():
182+
global hbase_env
183+
global java_home
184+
global java
185+
java_home = os.getenv('JAVA_HOME')
186+
java = 'java'
187+
188+
# HBase configuration folder path (where hbase-site.xml reside) for
189+
# HBase/Phoenix client side property override
190+
hbase_config_path = hbase_conf_dir
191+
192+
# load hbase-env.??? to extract JAVA_HOME, HBASE_PID_DIR, HBASE_LOG_DIR
193+
hbase_env_path = None
194+
hbase_env_cmd = None
195+
if os.name == 'posix':
196+
hbase_env_path = os.path.join(hbase_config_path, 'hbase-env.sh')
197+
hbase_env_cmd = ['bash', '-c', 'source %s && env' % hbase_env_path]
198+
elif os.name == 'nt':
199+
hbase_env_path = os.path.join(hbase_config_path, 'hbase-env.cmd')
200+
hbase_env_cmd = ['cmd.exe', '/c', 'call %s & set' % hbase_env_path]
201+
if not hbase_env_path or not hbase_env_cmd:
202+
sys.stderr.write("hbase-env file unknown on platform {}{}".format(os.name, os.linesep))
203+
sys.exit(-1)
204+
205+
hbase_env = {}
206+
if os.path.isfile(hbase_env_path):
207+
p = subprocess.Popen(hbase_env_cmd, stdout = subprocess.PIPE)
208+
for x in p.stdout:
209+
(k, _, v) = tryDecode(x).partition('=')
210+
hbase_env[k.strip()] = v.strip()
211+
212+
if 'JAVA_HOME' in hbase_env:
213+
java_home = hbase_env['JAVA_HOME']
214+
215+
if java_home:
216+
java = os.path.join(java_home, 'bin', 'java')
217+
218+
return ""
219+
220+
221+
def __set_jvm_flags():
222+
global jvm_module_flags
223+
jvm_module_flags = ""
224+
# This should be ASCII
225+
version_output = subprocess.check_output([java, "-version"], stderr=subprocess.STDOUT).decode()
226+
version_output = tryDecode(version_output)
227+
m = re.search(r'version\s"(\d+)\.(\d+)', version_output)
228+
if (m is None):
229+
# Could not find version
230+
return ""
231+
major = m.group(1)
232+
minor = m.group(2)
233+
if (major is None or minor is None):
234+
#Could not identify version
235+
return ""
236+
if (minor == "1"):
237+
major = minor
238+
if (int(major) >= 11):
239+
# Copied from hbase startup script
240+
jvm_module_flags = "-Dorg.apache.hbase.thirdparty.io.netty.tryReflectionSetAccessible=true \
241+
--add-modules jdk.unsupported \
242+
--add-opens java.base/java.nio=ALL-UNNAMED \
243+
--add-opens java.base/sun.nio.ch=ALL-UNNAMED \
244+
--add-opens java.base/java.lang=ALL-UNNAMED \
245+
--add-opens java.base/jdk.internal.ref=ALL-UNNAMED \
246+
--add-opens java.base/java.lang.reflect=ALL-UNNAMED \
247+
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
248+
--add-exports java.security.jgss/sun.security.krb5=ALL-UNNAMED \
249+
--add-exports java.base/sun.net.dns=ALL-UNNAMED \
250+
--add-exports java.base/sun.net.util=ALL-UNNAMED"
175251
return ""
176252

177253
def shell_quote(args):
@@ -208,3 +284,8 @@ def common_sqlline_args(parser):
208284
print("phoenix_thin_client_jar:", phoenix_thin_client_jar)
209285
print("sqlline_with_deps_jar", sqlline_with_deps_jar)
210286
print("slf4j_backend_jar:", slf4j_backend_jar)
287+
print("java_home:", java_home)
288+
print("java:", java)
289+
print("jvm_module_flags:", jvm_module_flags)
290+
print("hbase_env:", hbase_env)
291+

Diff for: bin/queryserver.py

+17-30
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,8 @@
8282
phoenix_out_file = '%s.out' % phoenix_file_basename
8383
phoenix_pid_file = '%s.pid' % phoenix_file_basename
8484

85-
# load hbase-env.??? to extract JAVA_HOME, HBASE_PID_DIR, HBASE_LOG_DIR
86-
hbase_env_path = None
87-
hbase_env_cmd = None
88-
if os.name == 'posix':
89-
hbase_env_path = os.path.join(hbase_conf_dir, 'hbase-env.sh')
90-
hbase_env_cmd = ['bash', '-c', 'source %s && env' % hbase_env_path]
91-
elif os.name == 'nt':
92-
hbase_env_path = os.path.join(hbase_conf_dir, 'hbase-env.cmd')
93-
hbase_env_cmd = ['cmd.exe', '/c', 'call %s & set' % hbase_env_path]
94-
if not hbase_env_path or not hbase_env_cmd:
95-
sys.stderr.write("hbase-env file unknown on platform {}{}".format(os.name, os.linesep))
96-
sys.exit(-1)
97-
98-
hbase_env = {}
99-
if os.path.isfile(hbase_env_path):
100-
p = subprocess.Popen(hbase_env_cmd, stdout = subprocess.PIPE)
101-
for x in p.stdout:
102-
(k, _, v) = tryDecode(x).partition('=')
103-
hbase_env[k.strip()] = v.strip()
104-
105-
java_home = hbase_env.get('JAVA_HOME') or os.getenv('JAVA_HOME')
106-
if java_home:
107-
java = os.path.join(java_home, 'bin', 'java')
108-
else:
109-
java = 'java'
110-
11185
tmp_dir = os.path.join(tempfile.gettempdir(), 'phoenix')
86+
hbase_env = phoenix_queryserver_utils.hbase_env
11287
opts = os.getenv('PHOENIX_QUERYSERVER_OPTS') or hbase_env.get('PHOENIX_QUERYSERVER_OPTS') or ''
11388
pid_dir = os.getenv('PHOENIX_QUERYSERVER_PID_DIR') or hbase_env.get('HBASE_PID_DIR') or tmp_dir
11489
log_dir = os.getenv('PHOENIX_QUERYSERVER_LOG_DIR') or hbase_env.get('HBASE_LOG_DIR') or tmp_dir
@@ -120,7 +95,7 @@
12095
# " -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true" + \
12196

12297
# The command is run through subprocess so environment variables are automatically inherited
123-
java_cmd = '%(java)s -cp ' +\
98+
java_cmd = '%(java)s %(jvm_module_flags)s -cp ' +\
12499
hbase_conf_dir + os.pathsep + \
125100
hadoop_conf_dir + os.pathsep + \
126101
phoenix_queryserver_utils.slf4j_backend_jar + os.pathsep + \
@@ -137,7 +112,11 @@
137112
" org.apache.phoenix.queryserver.server.QueryServer " + args
138113

139114
if command == 'makeWinServiceDesc':
140-
cmd = java_cmd % {'java': java, 'root_logger': 'INFO,DRFA,console', 'log_dir': log_dir, 'log_file': phoenix_log_file}
115+
cmd = (java_cmd % {'java': phoenix_queryserver_utils.java,
116+
'jvm_module_flags':phoenix_queryserver_utils.jvm_module_flags,
117+
'root_logger': 'INFO,DRFA,console',
118+
'log_dir': log_dir,
119+
'log_file': phoenix_log_file})
141120
slices = cmd.split(' ')
142121

143122
print("<service>")
@@ -174,7 +153,11 @@
174153
with context:
175154
# this block is the main() for the forked daemon process
176155
child = None
177-
cmd = java_cmd % {'java': java, 'root_logger': 'INFO,DRFA', 'log_dir': log_dir, 'log_file': phoenix_log_file}
156+
cmd = (java_cmd % {'java': phoenix_queryserver_utils.java,
157+
'jvm_module_flags':phoenix_queryserver_utils.jvm_module_flags,
158+
'root_logger': 'INFO,DRFA',
159+
'log_dir': log_dir,
160+
'log_file': phoenix_log_file})
178161

179162
# notify the child when we're killed
180163
def handler(signum, frame):
@@ -215,6 +198,10 @@ def handler(signum, frame):
215198

216199
else:
217200
# run in the foreground using defaults from log4j.properties
218-
cmd = java_cmd % {'java': java, 'root_logger': 'INFO,console', 'log_dir': '.', 'log_file': 'psql.log'}
201+
cmd = (java_cmd % {'java': phoenix_queryserver_utils.java,
202+
'jvm_module_flags':phoenix_queryserver_utils.jvm_module_flags,
203+
'root_logger': 'INFO,console',
204+
'log_dir': '.',
205+
'log_file': 'psql.log'})
219206
splitcmd = cmd.split()
220207
os.execvp(splitcmd[0], splitcmd)

Diff for: bin/sqlline-thin.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ def get_spnego_auth_disabled():
208208
and 'authentication=' not in jdbc_url and 'avatica_user=' not in jdbc_url):
209209
jdbc_url += ';authentication=SPNEGO'
210210

211-
java_cmd = java + ' $PHOENIX_OPTS ' + \
211+
java_cmd = phoenix_queryserver_utils.java + ' ' + phoenix_queryserver_utils.jvm_module_flags + \
212+
' $PHOENIX_OPTS ' + \
212213
' -cp "' + phoenix_queryserver_utils.sqlline_with_deps_jar + os.pathsep + \
213214
phoenix_queryserver_utils.phoenix_thin_client_jar + os.pathsep + \
214215
phoenix_queryserver_utils.slf4j_backend_jar + os.pathsep + \

0 commit comments

Comments
 (0)