Search in sources :

Example 1 with TestLogResponse

use of org.apache.hive.ptest.api.response.TestLogResponse in project hive by apache.

the class ExecutionController method testLog.

@RequestMapping(value = "/testLog", method = RequestMethod.POST)
@ResponseBody
public TestLogResponse testLog(@RequestBody TestLogRequest logsRequest, BindingResult result) {
    String testHandle = logsRequest.getTestHandle();
    Test testExecution = mTests.get(testHandle);
    if (result.hasErrors() || Strings.nullToEmpty(logsRequest.getTestHandle()).trim().isEmpty() || testExecution == null || logsRequest.getLength() > MAX_READ_SIZE) {
        return new TestLogResponse(Status.illegalArgument());
    }
    File outputFile = testExecution.getOutputFile();
    if (outputFile == null || logsRequest.getOffset() > outputFile.length()) {
        return new TestLogResponse(Status.illegalArgument());
    }
    RandomAccessFile fileHandle = null;
    try {
        fileHandle = new RandomAccessFile(outputFile, "r");
        long offset = logsRequest.getOffset();
        fileHandle.seek(offset);
        int readLength = 0;
        if (offset < fileHandle.length()) {
            readLength = (int) Math.min(fileHandle.length() - offset, logsRequest.getLength());
        }
        byte[] buffer = new byte[readLength];
        fileHandle.readFully(buffer);
        offset += readLength;
        return new TestLogResponse(Status.ok(), offset, new String(buffer, Charsets.UTF_8));
    } catch (IOException e) {
        LOG.info("Unexpected IO error reading " + testExecution.getOutputFile(), e);
        return new TestLogResponse(Status.internalError(e.getMessage()));
    } finally {
        if (fileHandle != null) {
            try {
                fileHandle.close();
            } catch (IOException e) {
                LOG.warn("Error closing " + outputFile, e);
            }
        }
    }
}
Also used : TestLogResponse(org.apache.hive.ptest.api.response.TestLogResponse) RandomAccessFile(java.io.RandomAccessFile) PTest(org.apache.hive.ptest.execution.PTest) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with TestLogResponse

use of org.apache.hive.ptest.api.response.TestLogResponse in project hive by apache.

the class PTestClient method post.

private <S extends GenericResponse> S post(Object payload, boolean agressiveRetry) throws Exception {
    EndPointResponsePair endPointResponse = Preconditions.checkNotNull(REQUEST_TO_ENDPOINT.get(payload.getClass()), payload.getClass().getName());
    HttpPost request = new HttpPost(mApiEndPoint + endPointResponse.getEndpoint());
    try {
        String payloadString = mMapper.writeValueAsString(payload);
        StringEntity params = new StringEntity(payloadString);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        if (agressiveRetry) {
            mHttpClient.setHttpRequestRetryHandler(new PTestHttpRequestRetryHandler());
        }
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new IllegalStateException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        @SuppressWarnings("unchecked") S result = (S) endPointResponse.getResponseClass().cast(mMapper.readValue(response, endPointResponse.getResponseClass()));
        Status.assertOK(result.getStatus());
        if (System.getProperty("DEBUG_PTEST_CLIENT") != null) {
            System.err.println("payload " + payloadString);
            if (result instanceof TestLogResponse) {
                System.err.println("response " + ((TestLogResponse) result).getOffset() + " " + ((TestLogResponse) result).getStatus());
            } else {
                System.err.println("response " + response);
            }
        }
        Thread.sleep(1000);
        return result;
    } finally {
        request.abort();
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) TestLogResponse(org.apache.hive.ptest.api.response.TestLogResponse) HttpResponse(org.apache.http.HttpResponse)

Example 3 with TestLogResponse

use of org.apache.hive.ptest.api.response.TestLogResponse in project hive by apache.

the class PTestClient method printLogs.

private long printLogs(String testHandle, long offset) throws Exception {
    TestLogRequest logsRequest = new TestLogRequest(testHandle, offset, 64 * 1024);
    TestLogResponse logsResponse = post(logsRequest, true);
    System.out.print(logsResponse.getBody());
    return logsResponse.getOffset();
}
Also used : TestLogResponse(org.apache.hive.ptest.api.response.TestLogResponse) TestLogRequest(org.apache.hive.ptest.api.request.TestLogRequest)

Aggregations

TestLogResponse (org.apache.hive.ptest.api.response.TestLogResponse)3 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 TestLogRequest (org.apache.hive.ptest.api.request.TestLogRequest)1 PTest (org.apache.hive.ptest.execution.PTest)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1