Search in sources :

Example 1 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.

the class BackupRequestsClient method streamRequest.

@Override
public void streamRequest(StreamRequest request, RequestContext requestContext, Callback<StreamResponse> callback) {
    // Currently only support backup requests with IS_FULL_REQUEST.
    if (!isFullRequest(requestContext)) {
        _d2Client.streamRequest(request, requestContext, callback);
        return;
    }
    if (!isBuffered(requestContext)) {
        final FullEntityObserver observer = new FullEntityObserver(new Callback<ByteString>() {

            @Override
            public void onError(Throwable e) {
                LOG.warn("Failed to record request's entity for retrying backup request.");
            }

            @Override
            public void onSuccess(ByteString result) {
                requestContext.putLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY, result);
            }
        });
        request.getEntityStream().addObserver(observer);
    }
    if (_isD2Async) {
        requestAsync(request, requestContext, _d2Client::streamRequest, callback);
        return;
    }
    _d2Client.streamRequest(request, requestContext, decorateCallbackSync(request, requestContext, _d2Client::streamRequest, callback));
}
Also used : ByteString(com.linkedin.data.ByteString) FullEntityObserver(com.linkedin.r2.message.stream.entitystream.FullEntityObserver)

Example 2 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.

the class TestEntityStream method testRaceBetweenDoneAndCancel.

private void testRaceBetweenDoneAndCancel(ExecutorService executor) throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(2);
    final CountDownLatch prepareLatch = new CountDownLatch(2);
    final TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    final ControlReader reader = new ControlReader();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1000);
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (writer.remaining() > 100) {
                writer.write();
            }
            prepareLatch.countDown();
            startLatch.await();
            writer.done();
            finishLatch.countDown();
            return null;
        }
    });
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            prepareLatch.countDown();
            startLatch.await();
            reader.cancel();
            finishLatch.countDown();
            return null;
        }
    });
    prepareLatch.await();
    startLatch.countDown();
    Assert.assertTrue(finishLatch.await(1000, TimeUnit.MILLISECONDS));
    // in any case, reader shouldn't fail
    Assert.assertEquals(reader.errorTimes(), 0);
    // if done wins the race
    if (reader.doneTimes() > 0) {
        Assert.assertEquals(reader.doneTimes(), 1);
        Assert.assertEquals(observer.doneTimes(), 1);
        Assert.assertEquals(observer.errorTimes(), 0);
        Assert.assertEquals(writer.abortedTimes(), 0);
    } else // if cancel wins the race
    {
        Assert.assertEquals(observer.doneTimes(), 0);
        Assert.assertEquals(observer.errorTimes(), 1);
        Assert.assertEquals(writer.abortedTimes(), 1);
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.

the class TestEntityStream method testObserverThrowRuntimeException.

/**
 * This test will check the correct behavior in case of a Runtime Exception for the observer.
 * Note the Runtime Exception is not the only unchecked exception, and we have to consider also Error
 * which is the other unchecked throwable
 */
@Test
public void testObserverThrowRuntimeException() {
    Observer observer = new TestObserver() {

        @Override
        public void onDone() {
            throw new RuntimeException("broken observer throws");
        }

        @Override
        public void onDataAvailable(ByteString data) {
            throw new RuntimeException("broken observer throws");
        }

        @Override
        public void onError(Throwable ex) {
            throw new RuntimeException("broken observer throws");
        }
    };
    Exception ex = new RuntimeException("writer has problem");
    testObserverThrow(observer, ex);
}
Also used : ByteString(com.linkedin.data.ByteString) Observer(com.linkedin.r2.message.stream.entitystream.Observer) Test(org.testng.annotations.Test)

Example 4 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.

the class TestEntityStream method testObserverThrow.

public void testObserverThrow(Observer observer, Throwable writeError) {
    TestWriter writer = new TestWriter();
    ControlReader reader = new ControlReader();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1);
    writer.write();
    writer.done();
    writer.error(writeError);
    Assert.assertEquals(writer.abortedTimes(), 0);
    Assert.assertEquals(reader.getChunkCount(), 1);
    Assert.assertEquals(reader.doneTimes(), 1);
    Assert.assertEquals(reader.errorTimes(), 0);
    writer = new TestWriter();
    reader = new ControlReader();
    es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1);
    writer.write();
    writer.error(writeError);
    Assert.assertEquals(writer.abortedTimes(), 0);
    Assert.assertEquals(reader.getChunkCount(), 1);
    Assert.assertEquals(reader.errorTimes(), 1);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream)

Example 5 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer in project rest.li by linkedin.

the class TestEntityStream method testRaceBetweenAbnormalAbortAndCancel.

private void testRaceBetweenAbnormalAbortAndCancel(ExecutorService executor) throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(2);
    final CountDownLatch prepareLatch = new CountDownLatch(2);
    final TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    final ControlReader reader = new ControlReader() {

        @Override
        public void onDataAvailable(ByteString data) {
            try {
                prepareLatch.countDown();
                startLatch.await();
            } catch (Exception ex) {
            // ...
            }
            throw new RuntimeException("broken reader throws");
        }
    };
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1000);
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (writer.remaining() > 0) {
                writer.write();
            }
            finishLatch.countDown();
            return null;
        }
    });
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            prepareLatch.countDown();
            startLatch.await();
            reader.cancel();
            finishLatch.countDown();
            return null;
        }
    });
    prepareLatch.await();
    startLatch.countDown();
    Assert.assertTrue(finishLatch.await(1000, TimeUnit.MILLISECONDS));
    // we should always fail because cancel on reader side wouldn't cause cancel action if
    // writer is already in the writing process
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 1);
    Assert.assertEquals(observer.getLastEvent(), "onError");
    Assert.assertEquals(reader.errorTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), 0);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)15 Test (org.testng.annotations.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ByteString (com.linkedin.data.ByteString)8 Observer (com.linkedin.r2.message.stream.entitystream.Observer)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 ExecutorService (java.util.concurrent.ExecutorService)4 ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)2 WriteHandle (com.linkedin.r2.message.stream.entitystream.WriteHandle)2 Callback (com.linkedin.common.callback.Callback)1 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)1 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)1 RequestContext (com.linkedin.r2.message.RequestContext)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 BaseConnector (com.linkedin.r2.message.stream.entitystream.BaseConnector)1 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)1 FullEntityObserver (com.linkedin.r2.message.stream.entitystream.FullEntityObserver)1 MessageType (com.linkedin.r2.transport.common.MessageType)1