Search in sources :

Example 6 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class SseWorkerConnectionTest method testStreamContentDrops.

@Test
public void testStreamContentDrops() throws Exception {
    SpectatorRegistryFactory.setRegistry(new DefaultRegistry());
    String metricGroupString = "testmetric";
    MetricGroupId metricGroupId = new MetricGroupId(metricGroupString);
    SseWorkerConnection workerConnection = new SseWorkerConnection("connection_type", "hostname", 80, b -> {
    }, b -> {
    }, t -> {
    }, 600, false, new CopyOnWriteArraySet<>(), 1, null, true, metricGroupId);
    HttpClientResponse<ServerSentEvent> response = mock(HttpClientResponse.class);
    TestScheduler testScheduler = Schedulers.test();
    // Events are just "0", "1", "2", ...
    Observable<ServerSentEvent> contentObs = Observable.interval(1, TimeUnit.SECONDS, testScheduler).map(t -> new ServerSentEvent(Unpooled.copiedBuffer(Long.toString(t), Charset.defaultCharset())));
    when(response.getContent()).thenReturn(contentObs);
    TestSubscriber<MantisServerSentEvent> subscriber = new TestSubscriber<>(1);
    workerConnection.streamContent(response, b -> {
    }, 600, "delimiter").subscribeOn(testScheduler).subscribe(subscriber);
    testScheduler.advanceTimeBy(100, TimeUnit.SECONDS);
    subscriber.assertValueCount(1);
    List<MantisServerSentEvent> events = subscriber.getOnNextEvents();
    assertEquals("0", events.get(0).getEventAsString());
    Metrics metrics = MetricsRegistry.getInstance().getMetric(metricGroupId);
    Counter onNextCounter = metrics.getCounter(DropOperator.Counters.onNext.toString());
    Counter droppedCounter = metrics.getCounter(DropOperator.Counters.dropped.toString());
    logger.info("next: {}", onNextCounter.value());
    logger.info("drop: {}", droppedCounter.value());
    assertTrue(onNextCounter.value() < 10);
    assertTrue(droppedCounter.value() > 90);
}
Also used : ServerSentEvent(mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) Metrics(io.mantisrx.common.metrics.Metrics) Counter(io.mantisrx.common.metrics.Counter) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) TestSubscriber(rx.observers.TestSubscriber) MetricGroupId(io.mantisrx.common.metrics.spectator.MetricGroupId) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 7 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class SourceJobWorkerMetricsSubscriptionTest method testGetResultsForAllSourceJobs.

@Test
public void testGetResultsForAllSourceJobs() throws Exception {
    List<SourceJobParameters.TargetInfo> infos = ImmutableList.of(new SourceJobParameters.TargetInfoBuilder().withSourceJobName("jobA").withQuery("criterion").withClientId("client1").build(), new SourceJobParameters.TargetInfoBuilder().withSourceJobName("jobA").withQuery("criterion").withClientId("client2").build(), new SourceJobParameters.TargetInfoBuilder().withSourceJobName("jobB").withQuery("criterion").withClientId("client1").build(), new SourceJobParameters.TargetInfoBuilder().withSourceJobName("jobB").withQuery("criterion").withClientId("client3").build());
    MantisMasterClientApi masterClient = mock(MantisMasterClientApi.class);
    SourceJobWorkerMetricsSubscription sub = spy(new SourceJobWorkerMetricsSubscription(infos, masterClient, null, new AutoScaleMetricsConfig()));
    when(masterClient.namedJobInfo("jobA")).thenReturn(Observable.just(new NamedJobInfo("jobA", "jobA-1")));
    when(masterClient.namedJobInfo("jobB")).thenReturn(Observable.just(new NamedJobInfo("jobA", "jobB-2")));
    doReturn(Observable.just(Observable.just(new MantisServerSentEvent("jobA-event")))).when(sub).getResultsForJobId(eq("jobA-1"), any());
    doReturn(Observable.just(Observable.just(new MantisServerSentEvent("jobB-event")))).when(sub).getResultsForJobId(eq("jobB-2"), any());
    CountDownLatch latch = new CountDownLatch(2);
    Observable.merge(sub.getResults()).doOnNext(event -> {
        if ("jobA-event".equals(event.getEventAsString()) || "jobB-event".equals(event.getEventAsString())) {
            latch.countDown();
        }
    }).subscribe();
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
    Set<String> jobAMetrics = ImmutableSet.of("PushServerSse:clientId=client1:*", "PushServerSse:clientId=client2:*", "ServerSentEventRequestHandler:clientId=client1:*", "ServerSentEventRequestHandler:clientId=client2:*");
    verify(sub, times(1)).getResultsForJobId("jobA-1", jobAMetrics);
    jobAMetrics = ImmutableSet.of("PushServerSse:clientId=client1:*", "PushServerSse:clientId=client3:*", "ServerSentEventRequestHandler:clientId=client1:*", "ServerSentEventRequestHandler:clientId=client3:*");
    verify(sub, times(1)).getResultsForJobId("jobB-2", jobAMetrics);
}
Also used : ImmutableSet(io.mantisrx.shaded.com.google.common.collect.ImmutableSet) SourceJobParameters(io.mantisrx.runtime.parameter.SourceJobParameters) MantisMasterClientApi(io.mantisrx.server.master.client.MantisMasterClientApi) Set(java.util.Set) Test(org.junit.Test) ImmutableMap(io.mantisrx.shaded.com.google.common.collect.ImmutableMap) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) Map(java.util.Map) Assert.assertEquals(org.junit.Assert.assertEquals) NamedJobInfo(io.mantisrx.server.core.NamedJobInfo) ImmutableList(io.mantisrx.shaded.com.google.common.collect.ImmutableList) SourceJobParameters(io.mantisrx.runtime.parameter.SourceJobParameters) CountDownLatch(java.util.concurrent.CountDownLatch) NamedJobInfo(io.mantisrx.server.core.NamedJobInfo) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) MantisMasterClientApi(io.mantisrx.server.master.client.MantisMasterClientApi) Test(org.junit.Test)

Example 8 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class SubmitEphemeralJob method main.

public static void main(String[] args) {
    try {
        Args.parse(SubmitEphemeralJob.class, args);
    } catch (IllegalArgumentException e) {
        Args.usage(SubmitEphemeralJob.class);
        System.exit(1);
    }
    Properties properties = new Properties();
    try (InputStream inputStream = new FileInputStream(propFile)) {
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    final CountDownLatch latch = new CountDownLatch(1);
    // AutoCloseable job will terminate job if onCloseKillJob() called when building it
    Subscription subscription1 = null;
    // Subscription s2=null;
    MantisSSEJob job1 = new MantisSSEJob.Builder(properties).name(jobName).onCloseKillJob().onConnectionReset(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            System.err.println("Reconnecting due to error: " + throwable.getMessage());
        }
    }).buildJobSubmitter();
    // .buildJobSubmitter();
    try {
        Observable<Observable<MantisServerSentEvent>> observable = job1.submitAndGet();
        subscription1 = observable.doOnNext(new Action1<Observable<MantisServerSentEvent>>() {

            @Override
            public void call(Observable<MantisServerSentEvent> o) {
                o.doOnNext(new Action1<MantisServerSentEvent>() {

                    @Override
                    public void call(MantisServerSentEvent data) {
                        logger.info("Got event: " + data);
                        latch.countDown();
                    }
                }).subscribe();
            }
        }).doOnCompleted(new Action0() {

            @Override
            public void call() {
                System.out.println("Observable completed!!!");
            }
        }).subscribe();
        if (latch.await(50, TimeUnit.SECONDS))
            System.out.println("SUCCESS");
        else
            System.out.println("FAILURE");
        // .subscribe();
        if (latch.await(50, TimeUnit.SECONDS))
            System.out.println("SUCCESS");
        else
            System.out.println("FAILURE");
        Thread.sleep(30000000);
        // unsubscribe to close connection to sink
        subscription1.unsubscribe();
        // s2.unsubscribe();
        job1.close();
    // job2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.exit(0);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) FileInputStream(java.io.FileInputStream) Observable(rx.Observable) IOException(java.io.IOException) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) Subscription(rx.Subscription) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 9 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class SubmitWithUniqueTag method main.

public static void main(String[] args) {
    try {
        Args.parse(SubmitWithUniqueTag.class, args);
    } catch (IllegalArgumentException e) {
        Args.usage(SubmitEphemeralJob.class);
        System.exit(1);
    }
    Properties properties = new Properties();
    try (InputStream inputStream = new FileInputStream(propFile)) {
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    final JobSla jobSla = new JobSla.Builder().withUniqueJobTagValue("foobar").build();
    MantisSSEJob job = new MantisSSEJob.Builder(properties).jobSla(jobSla).name(jobName).buildJobSubmitter();
    final Observable<Observable<MantisServerSentEvent>> o = job.submitAndGet();
    final CountDownLatch latch = new CountDownLatch(5);
    final Subscription subscribe = o.doOnNext(new Action1<Observable<MantisServerSentEvent>>() {

        @Override
        public void call(Observable<MantisServerSentEvent> eventObservable) {
            eventObservable.doOnNext(new Action1<MantisServerSentEvent>() {

                @Override
                public void call(MantisServerSentEvent event) {
                    System.out.println("event: " + event.getEventAsString());
                    latch.countDown();
                }
            }).subscribe();
        }
    }).subscribe();
    try {
        if (latch.await(50, TimeUnit.SECONDS))
            System.out.println("SUCCESS");
        else
            System.out.println("FAILURE");
        subscribe.unsubscribe();
        System.exit(0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Action1(rx.functions.Action1) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) FileInputStream(java.io.FileInputStream) Observable(rx.Observable) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) JobSla(io.mantisrx.runtime.JobSla) Subscription(rx.Subscription) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 10 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class CompressionUtils method tokenize_1.

static List<MantisServerSentEvent> tokenize_1(BufferedReader bf) throws IOException {
    StringBuilder sb = new StringBuilder();
    String line;
    List<MantisServerSentEvent> msseList = new ArrayList<>();
    String outStr = "";
    while ((line = bf.readLine()) != null) {
        sb.append(line);
    }
    int i = 0;
    outStr = sb.toString();
    sb = new StringBuilder();
    while (i < outStr.length()) {
        while (outStr.charAt(i) != '$') {
            sb.append(outStr.charAt(i));
            i++;
        }
        if (i + 3 < outStr.length()) {
            if (outStr.charAt(i) == '$' && outStr.charAt(i + 1) == '$' && outStr.charAt(i + 2) == '$') {
                i += 3;
                msseList.add(new MantisServerSentEvent(sb.toString()));
                sb = new StringBuilder();
            }
        } else {
            sb.append(outStr.charAt(i));
            i++;
        }
    }
    return msseList;
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ArrayList(java.util.ArrayList)

Aggregations

MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)17 IOException (java.io.IOException)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Action1 (rx.functions.Action1)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Observable (rx.Observable)6 Action0 (rx.functions.Action0)6 Subscription (rx.Subscription)5 MantisSSEJob (io.mantisrx.client.MantisSSEJob)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 BufferedReader (java.io.BufferedReader)3 JobSla (io.mantisrx.runtime.JobSla)2 StringReader (java.io.StringReader)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)1 SinkConnectionsStatus (io.mantisrx.client.SinkConnectionsStatus)1 Counter (io.mantisrx.common.metrics.Counter)1 Metrics (io.mantisrx.common.metrics.Metrics)1