Search in sources :

Example 6 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class TcpTransportTest method shouldExhaustRetries.

@Test
public void shouldExhaustRetries() throws TransportException, IOException {
    Sink sink = mock(Sink.class);
    TcpTransport transport = new TcpTransport(sink, 4, 0);
    TcpTransportBuffer transportBuffer = mock(TcpTransportBuffer.class);
    Buffer buffer = new Buffer();
    when(transportBuffer.getInternalBuffer()).thenReturn(buffer);
    doThrow(new IOException()).when(sink).write(eq(buffer), eq(0L));
    try {
        transport.sendBatch(transportBuffer);
        fail("Should exhaust retries");
    } catch (TransportException ex) {
        assertTrue(ex.getCause() instanceof RetriesExhaustedException);
    }
    verify(sink, times(5)).write(eq(buffer), eq(0L));
}
Also used : Buffer(okio.Buffer) Sink(okio.Sink) RetriesExhaustedException(com.evanlennick.retry4j.exception.RetriesExhaustedException) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException) Test(org.junit.Test)

Example 7 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class HttpTransport method sendBatchUncompressed.

protected HttpResponse sendBatchUncompressed(HttpPost httpPost, byte[] raw) throws TransportException {
    HttpEntity entity = new ByteArrayEntity(raw, getUncompressedContentType());
    httpPost.setEntity(entity);
    /*
     * Make call
     */
    HttpResponse resp = null;
    try {
        resp = this.client.execute(httpPost);
    } catch (IOException e) {
        throw new TransportException("failed to make call", e);
    }
    return resp;
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException)

Example 8 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class HttpTransport method sendBatchCompressed.

protected HttpResponse sendBatchCompressed(HttpPost httpPost, byte[] raw) throws TransportException {
    /*
     * Write gzip data to Entity and set content encoding to gzip
     */
    ByteArrayEntity entity = new ByteArrayEntity(raw, ContentType.DEFAULT_BINARY);
    entity.setContentEncoding("gzip");
    httpPost.addHeader(new BasicHeader("Accept-Encoding", "gzip"));
    httpPost.setEntity(entity);
    /*
     * Make call
     */
    HttpResponse resp = null;
    try {
        resp = this.client.execute(httpPost);
    } catch (IOException e) {
        throw new TransportException("failed to make call", e);
    }
    return resp;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException) BasicHeader(org.apache.http.message.BasicHeader)

Example 9 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class ElasticSearchTransport method checkResponse.

@Override
public void checkResponse(HttpResponse resp, String responseString) throws TransportException {
    /*
     * Check responses status code of the overall bulk call. The call can succeed but have
     * individual failures which are checked later.
     */
    if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase());
    }
    /*
     * Short circuit deserializing the response by just looking if there are any errors
     */
    if (responseString.contains("\"errors\":false")) {
        return;
    }
    /*
     * Convert response text to a POJO. Only tested with ES 2.4.x and 5.x.
     */
    Gson gson = new GsonBuilder().create();
    EsResponse esResp = null;
    try {
        esResp = gson.fromJson(responseString, EsResponse.class);
    } catch (JsonSyntaxException e) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase(), e);
    }
    /*
     * Look for the errors per index request
     */
    int failures = 0;
    if (esResp.items == null) {
        throw new TransportException("es call failed because " + resp.getStatusLine().getReasonPhrase());
    }
    HashSet<String> errorTypes = new HashSet<String>();
    for (Item item : esResp.items) {
        Index index = item.index;
        if (index == null || index.error == null || index.error.reason == null) {
            continue;
        }
        /*
       * For now just allow 200's and 400's. Both are considered non-fatal errors from the lambda's
       * perspective.
       */
        switch(index.status) {
            case HttpStatus.SC_OK:
            case HttpStatus.SC_BAD_REQUEST:
                continue;
            default:
                failures++;
                if (index.error != null && index.error.reason != null && index.error.type != null) {
                    if (!errorTypes.contains(index.error.type)) {
                        logger.error("Indexing Error Reason: " + index.error.reason);
                        if (index.error.caused_by != null) {
                            logger.error("Indexing Error Cause: " + index.error.caused_by.reason);
                        }
                        errorTypes.add(index.error.type);
                    }
                }
        }
    }
    errorTypes.clear();
    if (failures != 0) {
        throw new TransportException("es index failure count is " + failures);
    }
}
Also used : Item(com.nextdoor.bender.ipc.es.EsResponse.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Index(com.nextdoor.bender.ipc.es.EsResponse.Index) TransportException(com.nextdoor.bender.ipc.TransportException) HashSet(java.util.HashSet)

Example 10 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class BaseHandlerTest method testTransportOnSendFailure.

@Test(expected = TransportException.class)
public void testTransportOnSendFailure() throws Throwable {
    BaseHandler.CONFIG_FILE = "/config/handler_config.json";
    List<DummyEvent> events = new ArrayList<DummyEvent>(2);
    events.add(new DummyEvent("foo", 0));
    events.add(new DummyEvent("bar", 0));
    TestContext context = new TestContext();
    context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:tag");
    handler.init(context);
    IpcSenderService spyIpc = spy(handler.getIpcService());
    TransportFactory tf = spy(handler.getIpcService().getTransportFactory());
    BufferedTransporter mockTransport = mock(BufferedTransporter.class);
    doThrow(new TransportException("expected")).when(mockTransport).sendBatch(any());
    when(tf.newInstance()).thenReturn(mockTransport);
    spyIpc.setTransportFactory(tf);
    handler.setIpcService(spyIpc);
    try {
        handler.handler(events, context);
    } catch (Exception e) {
        throw e.getCause().getCause();
    }
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) TransportFactory(com.nextdoor.bender.ipc.TransportFactory) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) BufferedTransporter(com.nextdoor.bender.testutils.DummyTransportHelper.BufferedTransporter) TransportException(com.nextdoor.bender.ipc.TransportException) TransportException(com.nextdoor.bender.ipc.TransportException) IOException(java.io.IOException) DeserializationException(com.nextdoor.bender.deserializer.DeserializationException) SerializationException(com.nextdoor.bender.serializer.SerializationException) OperationException(com.nextdoor.bender.operation.OperationException) Test(org.junit.Test)

Aggregations

TransportException (com.nextdoor.bender.ipc.TransportException)20 IOException (java.io.IOException)16 Test (org.junit.Test)10 TestContext (com.nextdoor.bender.aws.TestContext)5 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)5 AmazonClientException (com.amazonaws.AmazonClientException)3 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)3 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)3 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)3 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)3 RetriesExhaustedException (com.evanlennick.retry4j.exception.RetriesExhaustedException)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 SerializationException (com.nextdoor.bender.serializer.SerializationException)3 ArrayList (java.util.ArrayList)3 HttpClient (org.apache.http.client.HttpClient)3 Context (com.amazonaws.services.lambda.runtime.Context)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 CallExecutor (com.evanlennick.retry4j.CallExecutor)2 UnexpectedException (com.evanlennick.retry4j.exception.UnexpectedException)2