-
Notifications
You must be signed in to change notification settings - Fork 24.2k
/
JVMCheck.java
139 lines (121 loc) · 6.06 KB
/
JVMCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.bootstrap;
import org.apache.lucene.util.Constants;
import org.elasticsearch.common.logging.Loggers;
import java.lang.management.ManagementFactory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/** Checks that the JVM is ok and won't cause index corruption */
public class JVMCheck {
/**
* URL with latest JVM recommendations
*/
static final String JVM_RECOMMENDATIONS = "http://www.elastic.co/guide/en/elasticsearch/reference/current/_installation.html";
/**
* System property which if set causes us to bypass the check completely (but issues a warning in doing so)
*/
static final String JVM_BYPASS = "es.bypass.vm.check";
/**
* Metadata and messaging for hotspot bugs.
*/
static class HotspotBug {
/** OpenJDK bug URL */
final String bugUrl;
/** Compiler workaround flag (null if there is no workaround) */
final String workAround;
HotspotBug(String bugUrl, String workAround) {
this.bugUrl = bugUrl;
this.workAround = workAround;
}
/** Returns an error message to the user for a broken version */
String getErrorMessage() {
StringBuilder sb = new StringBuilder();
sb.append("Java version: ").append(Constants.JAVA_VERSION);
sb.append(" suffers from critical bug ").append(bugUrl);
sb.append(" which can cause data corruption.");
sb.append(System.lineSeparator());
sb.append("Please upgrade the JVM, see ").append(JVM_RECOMMENDATIONS);
sb.append(" for current recommendations.");
if (workAround != null) {
sb.append(System.lineSeparator());
sb.append("If you absolutely cannot upgrade, please add ").append(workAround);
sb.append(" to the JVM_OPTS environment variable.");
sb.append(System.lineSeparator());
sb.append("Upgrading is preferred, this workaround will result in degraded performance.");
}
return sb.toString();
}
/** Warns the user when a workaround is being used to dodge the bug */
String getWarningMessage() {
StringBuilder sb = new StringBuilder();
sb.append("Workaround flag ").append(workAround);
sb.append(" for bug ").append(bugUrl);
sb.append(" found. ");
sb.append(System.lineSeparator());
sb.append("This will result in degraded performance!");
sb.append(System.lineSeparator());
sb.append("Upgrading is preferred, see ").append(JVM_RECOMMENDATIONS);
sb.append(" for current recommendations.");
return sb.toString();
}
}
/** mapping of hotspot version to hotspot bug information for the most serious bugs */
static final Map<String,HotspotBug> JVM_BROKEN_HOTSPOT_VERSIONS;
static {
Map<String,HotspotBug> bugs = new HashMap<>();
// 1.7.0: loop optimizer bug
bugs.put("21.0-b17", new HotspotBug("https://bugs.openjdk.java.net/browse/JDK-7070134", "-XX:-UseLoopPredicate"));
// register allocation issues (technically only x86/amd64). This impacted update 40, 45, and 51
bugs.put("24.0-b56", new HotspotBug("https://bugs.openjdk.java.net/browse/JDK-8024830", "-XX:-UseSuperWord"));
bugs.put("24.45-b08", new HotspotBug("https://bugs.openjdk.java.net/browse/JDK-8024830", "-XX:-UseSuperWord"));
bugs.put("24.51-b03", new HotspotBug("https://bugs.openjdk.java.net/browse/JDK-8024830", "-XX:-UseSuperWord"));
JVM_BROKEN_HOTSPOT_VERSIONS = Collections.unmodifiableMap(bugs);
}
/**
* Checks that the current JVM is "ok". This means it doesn't have severe bugs that cause data corruption.
*/
static void check() {
if (Boolean.parseBoolean(System.getProperty(JVM_BYPASS))) {
Loggers.getLogger(JVMCheck.class).warn("bypassing jvm version check for version [{}], this can result in data corruption!", Constants.JAVA_VERSION);
} else if ("Oracle Corporation".equals(Constants.JVM_VENDOR)) {
HotspotBug bug = JVM_BROKEN_HOTSPOT_VERSIONS.get(Constants.JVM_VERSION);
if (bug != null) {
if (bug.workAround != null && ManagementFactory.getRuntimeMXBean().getInputArguments().contains(bug.workAround)) {
Loggers.getLogger(JVMCheck.class).warn(bug.getWarningMessage());
} else {
throw new RuntimeException(bug.getErrorMessage());
}
}
} else if ("IBM Corporation".equals(Constants.JVM_VENDOR)) {
// currently any JVM from IBM will easily result in index corruption.
StringBuilder sb = new StringBuilder();
sb.append("IBM runtimes suffer from several bugs which can cause data corruption.");
sb.append(System.lineSeparator());
sb.append("Please upgrade the JVM, see ").append(JVM_RECOMMENDATIONS);
sb.append(" for current recommendations.");
throw new RuntimeException(sb.toString());
}
}
/** Command line driver for convenience */
public static void main(String args[]) {
check();
}
}