Search in sources :

Example 1 with MesosFileChunkObject

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);
}
Also used : MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) Test(org.junit.Test)

Example 2 with MesosFileChunkObject

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);
}
Also used : MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) Test(org.junit.Test)

Example 3 with MesosFileChunkObject

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);
}
Also used : MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) Test(org.junit.Test)

Example 4 with MesosFileChunkObject

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);
}
Also used : MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) Test(org.junit.Test)

Example 5 with MesosFileChunkObject

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject)

Aggregations

MesosFileChunkObject (com.hubspot.mesos.json.MesosFileChunkObject)10 Test (org.junit.Test)7 Pattern (java.util.regex.Pattern)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SingularitySandboxFile (com.hubspot.singularity.SingularitySandboxFile)1 SingularityTaskHistory (com.hubspot.singularity.SingularityTaskHistory)1 SlaveNotFoundException (com.hubspot.singularity.data.SandboxManager.SlaveNotFoundException)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 File (java.io.File)1 Reader (java.io.Reader)1 ByteBuffer (java.nio.ByteBuffer)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 Matcher (java.util.regex.Matcher)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1