Skip to content

Commit 3c0ea7a

Browse files
author
Hendrik Muhs
committed
refactor version information retrieval and use it for crash logging
1 parent c7ba65b commit 3c0ea7a

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/NativeController.java

+1-16
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.concurrent.TimeoutException;
25-
import java.util.regex.Matcher;
26-
import java.util.regex.Pattern;
2725

2826

2927
/**
@@ -84,20 +82,7 @@ public long getPid() throws TimeoutException {
8482
}
8583

8684
public Map<String, Object> getNativeCodeInfo() throws TimeoutException {
87-
String copyrightMessage = cppLogHandler.getCppCopyright(CONTROLLER_CONNECT_TIMEOUT);
88-
Matcher matcher = Pattern.compile("Version (.+) \\(Build ([^)]+)\\) Copyright ").matcher(copyrightMessage);
89-
if (matcher.find()) {
90-
Map<String, Object> info = new HashMap<>(2);
91-
info.put("version", matcher.group(1));
92-
info.put("build_hash", matcher.group(2));
93-
return info;
94-
} else {
95-
// If this happens it probably means someone has changed the format in lib/ver/CBuildInfo.cc
96-
// in the machine-learning-cpp repo without changing the pattern above to match
97-
String msg = "Unexpected native controller process copyright format: " + copyrightMessage;
98-
LOGGER.error(msg);
99-
throw new ElasticsearchException(msg);
100-
}
85+
return cppLogHandler.getNativeCodeInfo(CONTROLLER_CONNECT_TIMEOUT);
10186
}
10287

10388
public void startProcess(List<String> command) throws IOException {

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/logging/CppLogMessageHandler.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.logging.log4j.Level;
99
import org.apache.logging.log4j.Logger;
1010
import org.apache.logging.log4j.message.ParameterizedMessage;
11-
import org.elasticsearch.common.ParsingException;
11+
import org.elasticsearch.ElasticsearchException;
1212
import org.elasticsearch.common.Strings;
1313
import org.elasticsearch.common.bytes.BytesArray;
1414
import org.elasticsearch.common.bytes.BytesReference;
@@ -30,10 +30,15 @@
3030
import java.time.Instant;
3131
import java.time.temporal.ChronoUnit;
3232
import java.util.Deque;
33+
import java.util.HashMap;
34+
import java.util.Locale;
35+
import java.util.Map;
3336
import java.util.Objects;
3437
import java.util.concurrent.CountDownLatch;
3538
import java.util.concurrent.TimeUnit;
3639
import java.util.concurrent.TimeoutException;
40+
import java.util.regex.Matcher;
41+
import java.util.regex.Pattern;
3742

3843
/**
3944
* Handle a stream of C++ log messages that arrive via a named pipe in JSON format.
@@ -181,6 +186,26 @@ public String getCppCopyright(Duration timeout) throws TimeoutException {
181186
return cppCopyright;
182187
}
183188

189+
/**
190+
* Extracts version information from the copyright string which assumes a certain format.
191+
*/
192+
public Map<String, Object> getNativeCodeInfo(Duration timeout) throws TimeoutException {
193+
String copyrightMessage = getCppCopyright(timeout);
194+
Matcher matcher = Pattern.compile("Version (.+) \\(Build ([^)]+)\\) Copyright ").matcher(copyrightMessage);
195+
if (matcher.find()) {
196+
Map<String, Object> info = new HashMap<>(2);
197+
info.put("version", matcher.group(1));
198+
info.put("build_hash", matcher.group(2));
199+
return info;
200+
} else {
201+
// If this happens it probably means someone has changed the format in lib/ver/CBuildInfo.cc
202+
// in the machine-learning-cpp repo without changing the pattern above to match
203+
String msg = "Unexpected native process copyright format: " + copyrightMessage;
204+
LOGGER.error(msg);
205+
throw new ElasticsearchException(msg);
206+
}
207+
}
208+
184209
/**
185210
* Expected to be called very infrequently.
186211
*/
@@ -281,13 +306,14 @@ private void parseMessage(XContent xContent, BytesReference bytesRef) {
281306
} catch (XContentParseException e) {
282307
String upstreamMessage = "Fatal error: '" + bytesRef.utf8ToString() + "'";
283308
if (upstreamMessage.contains("bad_alloc")) {
284-
upstreamMessage += ", process ran out of memory.";
309+
upstreamMessage += ", process ran out of memory";
285310
}
286311

287312
// add version information, so it's conveniently next to the crash log
288313
upstreamMessage += ", version: ";
289314
try {
290-
upstreamMessage += getCppCopyright(Duration.ofMillis(10));
315+
Map<String, Object> versionInfo = getNativeCodeInfo(Duration.ofMillis(10));
316+
upstreamMessage += String.format(Locale.ROOT, "%s (build %s)", versionInfo.get("version"), versionInfo.get("build_hash"));
291317
} catch (TimeoutException timeoutException) {
292318
upstreamMessage += "failed to retrieve";
293319
}

0 commit comments

Comments
 (0)