Search in sources :

Example 1 with TransportBuffer

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

the class BaseHandlerTest method testIpcOnAddFailure.

@Test
public void testIpcOnAddFailure() throws Throwable {
    BaseHandler.CONFIG_FILE = "/config/handler_config.json";
    handler.skipWriteStats = true;
    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);
    TransportBuffer tbSpy1 = spy(new ArrayTransportBuffer());
    TransportBuffer tbSpy2 = spy(new ArrayTransportBuffer());
    doCallRealMethod().doCallRealMethod().when(tbSpy1).add(any());
    doThrow(new IllegalStateException("expected")).when(tbSpy2).add(any());
    IpcSenderService spyIpc = spy(handler.getIpcService());
    TransportFactory tfSpy = spy(spyIpc.getTransportFactory());
    when(tfSpy.newTransportBuffer()).thenReturn(tbSpy1, tbSpy2);
    spyIpc.setTransportFactory(tfSpy);
    handler.setIpcService(spyIpc);
    handler.handler(events, context);
    assertEquals(1, spyIpc.getSuccessCountStat().getValue());
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) TransportFactory(com.nextdoor.bender.ipc.TransportFactory) TestContext(com.nextdoor.bender.aws.TestContext) ArrayList(java.util.ArrayList) ArrayTransportBuffer(com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer) TransportBuffer(com.nextdoor.bender.ipc.TransportBuffer) ArrayTransportBuffer(com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer) Test(org.junit.Test)

Example 2 with TransportBuffer

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

the class S3Transport method sendBatch.

@Override
public void sendBatch(TransportBuffer buffer, LinkedHashMap<String, String> partitions, Context context) throws TransportException {
    S3TransportBuffer buf = (S3TransportBuffer) buffer;
    /*
     * Create s3 key (filepath + filename)
     */
    LinkedHashMap<String, String> parts = new LinkedHashMap<String, String>(partitions);
    String filename = parts.remove(FILENAME_KEY);
    if (filename == null) {
        filename = context.getAwsRequestId();
    }
    String key = parts.entrySet().stream().map(s -> s.getKey() + "=" + s.getValue()).collect(Collectors.joining("/"));
    key = (key.equals("") ? filename : key + '/' + filename);
    if (this.basePath.endsWith("/")) {
        key = this.basePath + key;
    } else {
        key = this.basePath + '/' + key;
    }
    // TODO: make this dynamic
    if (key.endsWith(".gz")) {
        key = key.substring(0, key.length() - 3);
    }
    /*
     * Add or strip out compression format extension
     *
     * TODO: get this based on the compression codec
     */
    if (this.compress || buf.isCompressed()) {
        key += ".bz2";
    }
    ByteArrayOutputStream os = buf.getInternalBuffer();
    /*
     * Compress stream if needed. Don't compress a compressed stream.
     */
    ByteArrayOutputStream payload;
    if (this.compress && !buf.isCompressed()) {
        payload = compress(os);
    } else {
        payload = os;
    }
    /*
     * For memory efficiency convert the output stream into an InputStream. This is done using the
     * easystream library but under the hood it uses piped streams to facilitate this process. This
     * avoids copying the entire contents of the OutputStream to populate the InputStream. Note that
     * this process creates another thread to consume from the InputStream.
     */
    final String s3Key = key;
    /*
     * Write to OutputStream
     */
    final InputStreamFromOutputStream<String> isos = new InputStreamFromOutputStream<String>() {

        public String produce(final OutputStream dataSink) throws Exception {
            /*
         * Note this is executed in a different thread
         */
            payload.writeTo(dataSink);
            return null;
        }
    };
    /*
     * Consume InputStream
     */
    try {
        sendStream(isos, s3Key, payload.size());
    } finally {
        try {
            isos.close();
        } catch (IOException e) {
            throw new TransportException(e);
        } finally {
            buf.close();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) PartitionedTransport(com.nextdoor.bender.ipc.PartitionedTransport) BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Context(com.amazonaws.services.lambda.runtime.Context) IOException(java.io.IOException) InputStreamFromOutputStream(com.gc.iotools.stream.is.InputStreamFromOutputStream) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) Collectors(java.util.stream.Collectors) LinkedHashMap(java.util.LinkedHashMap) Logger(org.apache.log4j.Logger) TransportBuffer(com.nextdoor.bender.ipc.TransportBuffer) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) TransportException(com.nextdoor.bender.ipc.TransportException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Map(java.util.Map) InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) AmazonClientException(com.amazonaws.AmazonClientException) InputStream(java.io.InputStream) InputStreamFromOutputStream(com.gc.iotools.stream.is.InputStreamFromOutputStream) OutputStream(java.io.OutputStream) BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) InputStreamFromOutputStream(com.gc.iotools.stream.is.InputStreamFromOutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with TransportBuffer

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

the class TcpTransport method sendBatch.

@Override
public void sendBatch(TransportBuffer buffer) throws TransportException {
    Buffer internalBuffer = ((TcpTransportBuffer) buffer).getInternalBuffer();
    Callable<Boolean> write = () -> {
        try {
            sink.write(internalBuffer, internalBuffer.size());
            return true;
        } catch (IOException ex) {
            throw new TransportException("Error while sending in tcp transport", ex);
        }
    };
    try {
        new CallExecutor(retryConfig).execute(write);
    } catch (RetriesExhaustedException | UnexpectedException ue) {
        throw new TransportException(ue);
    }
}
Also used : Buffer(okio.Buffer) TransportBuffer(com.nextdoor.bender.ipc.TransportBuffer) CallExecutor(com.evanlennick.retry4j.CallExecutor) UnexpectedException(com.evanlennick.retry4j.exception.UnexpectedException) RetriesExhaustedException(com.evanlennick.retry4j.exception.RetriesExhaustedException) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException)

Aggregations

TransportBuffer (com.nextdoor.bender.ipc.TransportBuffer)3 TransportException (com.nextdoor.bender.ipc.TransportException)2 IOException (java.io.IOException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 Context (com.amazonaws.services.lambda.runtime.Context)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)1 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)1 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)1 CallExecutor (com.evanlennick.retry4j.CallExecutor)1 RetriesExhaustedException (com.evanlennick.retry4j.exception.RetriesExhaustedException)1 UnexpectedException (com.evanlennick.retry4j.exception.UnexpectedException)1 InputStreamFromOutputStream (com.gc.iotools.stream.is.InputStreamFromOutputStream)1 TestContext (com.nextdoor.bender.aws.TestContext)1 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)1 PartitionedTransport (com.nextdoor.bender.ipc.PartitionedTransport)1 TransportFactory (com.nextdoor.bender.ipc.TransportFactory)1 ArrayTransportBuffer (com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer)1