use of com.hubspot.mesos.json.MesosFileChunkObject in project Singularity by HubSpot.
the class SandboxManagerTest method testValidUtf8WithThreeByteCharacters.
@Test
public void testValidUtf8WithThreeByteCharacters() throws IOException {
// data contains two ☃ characters
byte[] bytes = toBytes(JSON_START, SNOWMAN_UTF8_BYTES, SNOWMAN_UTF8_BYTES, JSON_END);
MesosFileChunkObject chunk = sandboxManager.parseResponseBody(response(bytes));
// nothing should be dropped
assertThat(chunk.getData()).isEqualTo(SNOWMAN + SNOWMAN);
assertThat(chunk.getOffset()).isEqualTo(DEFAULT_OFFSET);
}
use of com.hubspot.mesos.json.MesosFileChunkObject in project Singularity by HubSpot.
the class SandboxManagerTest method testInvalidUtf8WithLastByte.
@Test
public void testInvalidUtf8WithLastByte() throws IOException {
// data contains last byte of a fire character and a ☃ character
byte[] bytes = toBytes(JSON_START, THIRD_BALLOON_BYTE, SNOWMAN_UTF8_BYTES, JSON_END);
MesosFileChunkObject chunk = sandboxManager.parseResponseBody(response(bytes));
// the partial fire should be dropped and the offset should be advanced by one byte
assertThat(chunk.getData()).isEqualTo(SNOWMAN);
assertThat(chunk.getOffset()).isEqualTo(DEFAULT_OFFSET + 1);
}
use of com.hubspot.mesos.json.MesosFileChunkObject in project Singularity by HubSpot.
the class SandboxManagerTest method testInvalidUtf8WithOneByte.
@Test
public void testInvalidUtf8WithOneByte() throws IOException {
// data contains the last middle byte of a fire character
byte[] bytes = toBytes(JSON_START, SECOND_BALLOON_BYTE, JSON_END);
MesosFileChunkObject chunk = sandboxManager.parseResponseBody(response(bytes));
// the partial fire should be dropped and the offset should be advanced by one byte
assertThat(chunk.getData()).isEqualTo("");
assertThat(chunk.getOffset()).isEqualTo(DEFAULT_OFFSET + 1);
}
use of com.hubspot.mesos.json.MesosFileChunkObject in project Singularity by HubSpot.
the class SandboxManagerTest method testInvalidUtf8WithTwoBytes.
@Test
public void testInvalidUtf8WithTwoBytes() throws IOException {
// data contains the last two bytes of a fire character
byte[] bytes = toBytes(JSON_START, SECOND_BALLOON_BYTE, THIRD_BALLOON_BYTE, JSON_END);
MesosFileChunkObject chunk = sandboxManager.parseResponseBody(response(bytes));
// the partial fire should be dropped and the offset should be advanced by two bytes
assertThat(chunk.getData()).isEqualTo("");
assertThat(chunk.getOffset()).isEqualTo(DEFAULT_OFFSET + 2);
}
use of com.hubspot.mesos.json.MesosFileChunkObject in project Singularity by HubSpot.
the class MailTemplateHelpers method getMaybeTaskLogReadOffset.
// Searches through the file, looking for first error
private Optional<Long> getMaybeTaskLogReadOffset(final String slaveHostname, final String fullPath, final Long logLength, Optional<SingularityTask> task) {
long offset = 0;
long maxOffset = smtpConfiguration.get().getMaxTaskLogSearchOffset();
Optional<Pattern> maybePattern = getLogErrorRegex(task);
Pattern pattern;
if (maybePattern.isPresent()) {
pattern = maybePattern.get();
} else {
LOG.trace("Could not get regex pattern. Reading from bottom of file instead.");
return getMaybeTaskLogEndOfFileOffset(slaveHostname, fullPath, logLength);
}
// Get extra so that we can be sure to find the error
long length = logLength + pattern.toString().length();
Optional<MesosFileChunkObject> logChunkObject;
Optional<MesosFileChunkObject> previous = Optional.absent();
while (offset <= maxOffset) {
try {
logChunkObject = sandboxManager.read(slaveHostname, fullPath, Optional.of(offset), Optional.of(length));
} catch (RuntimeException e) {
LOG.error("Sandboxmanager failed to read {} on slave {}", fullPath, slaveHostname, e);
return Optional.absent();
}
if (logChunkObject.isPresent()) {
if (logChunkObject.get().getData().equals("")) {
// Passed end of file
if (previous.isPresent()) {
// If there was any log, get the bottom bytes of it
long end = previous.get().getOffset() + previous.get().getData().length();
return Optional.of(end - logLength);
}
return Optional.absent();
}
Matcher matcher = pattern.matcher(logChunkObject.get().getData());
if (matcher.find()) {
return Optional.of(offset + matcher.start());
} else {
offset += logLength;
}
} else {
// Couldn't read anything
LOG.error("Failed to read log file {}", fullPath);
return Optional.absent();
}
previous = logChunkObject;
}
LOG.trace("Searched through the first {} bytes of file {} and didn't find an error. Tailing bottom of file instead", maxOffset, fullPath);
return getMaybeTaskLogEndOfFileOffset(slaveHostname, fullPath, logLength);
}
Aggregations