Search in sources :

Example 1 with ServerContext

use of io.mantisrx.runtime.source.http.impl.ServerContext in project mantis by Netflix.

the class HttpSources method pollingSource.

/**
 * Create a {@code HttpSource} that infinitely polls the give server with the given URL
 *
 * @param host The host name of the server to be queried
 * @param port The port used for query
 * @param uri  The URI used for query. The URI should be relative to http://host:port/
 *
 * @return A builder that will return an implementation that polls the give server infinitely
 */
public static Builder<ByteBuf, String> pollingSource(final String host, final int port, String uri) {
    HttpClientFactory<ByteBuf, ByteBuf> clientFactory = HttpClientFactories.defaultFactory();
    HttpSourceImpl.Builder<ByteBuf, ByteBuf, String> builderImpl = HttpSourceImpl.builder(clientFactory, HttpRequestFactories.createGetFactory(uri), new Func2<ServerContext<HttpClientResponse<ByteBuf>>, ByteBuf, String>() {

        @Override
        public String call(ServerContext<HttpClientResponse<ByteBuf>> context, ByteBuf content) {
            return content.toString(Charset.defaultCharset());
        }
    }).withServerProvider(new HttpServerProvider() {

        @Override
        public Observable<ServerInfo> getServersToAdd() {
            return Observable.just(new ServerInfo(host, port));
        }

        @Override
        public Observable<ServerInfo> getServersToRemove() {
            return Observable.empty();
        }
    }).resumeWith(new ClientResumePolicy<ByteBuf, ByteBuf>() {

        @Override
        public Observable<HttpClientResponse<ByteBuf>> onError(ServerClientContext<ByteBuf, ByteBuf> clientContext, int attempts, Throwable error) {
            return clientContext.newResponse();
        }

        @Override
        public Observable<HttpClientResponse<ByteBuf>> onCompleted(ServerClientContext<ByteBuf, ByteBuf> clientContext, int attempts) {
            return clientContext.newResponse();
        }
    });
    return HttpSource.builder(builderImpl);
}
Also used : ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) ServerContext(io.mantisrx.runtime.source.http.impl.ServerContext) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) HttpSourceImpl(io.mantisrx.runtime.source.http.impl.HttpSourceImpl)

Example 2 with ServerContext

use of io.mantisrx.runtime.source.http.impl.ServerContext in project mantis by Netflix.

the class ContextualHttpSourceTest method canStreamFromMultipleServersWithCorrectContext.

@Test
public void canStreamFromMultipleServersWithCorrectContext() throws Exception {
    ContextualHttpSource<ServerSentEvent> source = HttpSources.contextualSource(HttpClientFactories.sseClientFactory(), HttpRequestFactories.createGetFactory("test/stream")).withServerProvider(localServerProvider).withActivityObserver(sourceObserver).build();
    final AtomicInteger counter = new AtomicInteger();
    final CountDownLatch done = new CountDownLatch(1);
    final ConcurrentHashMap<String, AtomicInteger> result = new ConcurrentHashMap<>();
    final CopyOnWriteArraySet<ServerInfo> connectedServers = new CopyOnWriteArraySet<>();
    Observable.merge(source.call(new Context(), new Index(1, 1))).doOnNext(new Action1<ServerContext<ServerSentEvent>>() {

        @Override
        public void call(ServerContext<ServerSentEvent> pair) {
            assertTrue(pair.getValue().contentAsString().contains("line"));
            counter.incrementAndGet();
            String msg = pair.getValue().contentAsString();
            result.putIfAbsent(msg, new AtomicInteger());
            result.get(msg).incrementAndGet();
            connectedServers.add(pair.getServer());
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            fail("Unexpected failure: " + throwable);
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            System.out.println("completed");
        }
    }).doAfterTerminate(new Action0() {

        @Override
        public void call() {
            done.countDown();
        }
    }).subscribe();
    long waitSeconds = 3;
    boolean timedout = !done.await(waitSeconds, TimeUnit.SECONDS);
    if (timedout) {
        fail(String.format("Waited at least %d seconds for the test to finish. Something is wrong", waitSeconds));
    }
    assertEquals("There should be as many as provided servers", localServerProvider.serverSize(), connectedServers.size());
    Assert.assertEquals(String.format("%d servers => the result has %d times of a single stream", localServerProvider.serverSize(), localServerProvider.serverSize()), counter.get(), RequestProcessor.smallStreamContent.size() * localServerProvider.serverSize());
    for (String data : RequestProcessor.smallStreamContent) {
        assertEquals(String.format("%d servers => %d identical copies per message", localServerProvider.serverSize(), localServerProvider.serverSize()), localServerProvider.serverSize(), result.get(data).get());
    }
    for (ServerInfo server : localServerProvider.getServerInfos()) {
        assertEquals("There should be one completion per server", 1, sourceObserver.getCount(server, EventType.SOURCE_COMPLETED));
        assertEquals("There should be one un-subscription per server", 1, sourceObserver.getCount(server, EventType.CONNECTION_UNSUBSCRIBED));
        assertEquals("There should be no error", 0, sourceObserver.getCount(server, EventType.SUBSCRIPTION_FAILED));
        assertEquals("There should be one connection per server", 1, sourceObserver.getCount(server, EventType.CONNECTION_ESTABLISHED));
    }
    assertEquals("There should be one completions", 1, sourceObserver.getCompletionCount());
    assertEquals(0, sourceObserver.getErrorCount());
    Set<EventType> events = sourceObserver.getEvents();
    assertEquals(EXPECTED_EVENTS_SETS, events);
    for (EventType event : events) {
        assertEquals("Each event should be recorded exactly once per server", localServerProvider.serverSize(), sourceObserver.getEventCount(event));
    }
}
Also used : Context(io.mantisrx.runtime.Context) ServerContext(io.mantisrx.runtime.source.http.impl.ServerContext) Action0(rx.functions.Action0) Action1(rx.functions.Action1) EventType(io.mantisrx.runtime.source.http.impl.HttpSourceImpl.HttpSourceEvent.EventType) ServerInfo(mantis.io.reactivex.netty.client.RxClient.ServerInfo) ServerSentEvent(mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent) Index(io.mantisrx.runtime.source.Index) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) ServerContext(io.mantisrx.runtime.source.http.impl.ServerContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

ServerContext (io.mantisrx.runtime.source.http.impl.ServerContext)2 ServerInfo (mantis.io.reactivex.netty.client.RxClient.ServerInfo)2 Context (io.mantisrx.runtime.Context)1 Index (io.mantisrx.runtime.source.Index)1 HttpSourceImpl (io.mantisrx.runtime.source.http.impl.HttpSourceImpl)1 EventType (io.mantisrx.runtime.source.http.impl.HttpSourceImpl.HttpSourceEvent.EventType)1 ByteBuf (io.netty.buffer.ByteBuf)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpClientResponse (mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse)1 ServerSentEvent (mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent)1 Test (org.junit.Test)1 Observable (rx.Observable)1 Action0 (rx.functions.Action0)1 Action1 (rx.functions.Action1)1