Search in sources :

Example 1 with MantisServerSentEvent

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

the class CompressionUtilsTest method shouldTokenizeWithEventsContainingPartialDelimiterMatchesWithCustomDelimiter.

@Test
public void shouldTokenizeWithEventsContainingPartialDelimiterMatchesWithCustomDelimiter() {
    String delimiter = UUID.randomUUID().toString();
    String event1 = "ab" + delimiter.substring(0, 9) + "cdef";
    String event2 = "ghi" + delimiter.substring(0, 5) + "jkl";
    String event3 = "lmno" + delimiter.substring(0, 4) + "pqrst" + delimiter.substring(0, 2);
    String testInput = event1 + delimiter + event2 + delimiter + event3;
    try (BufferedReader reader = new BufferedReader(new StringReader(testInput))) {
        List<MantisServerSentEvent> result = CompressionUtils.tokenize(reader, delimiter);
        assertEquals("Delimiter: '" + delimiter + "'", result.size(), 3);
        assertEquals(result.get(0).getEventAsString(), event1);
        assertEquals(result.get(1).getEventAsString(), event2);
        assertEquals(result.get(2).getEventAsString(), event3);
    } catch (IOException ex) {
        Assert.fail("Tokenization threw an IO exception that was unexpected");
    }
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with MantisServerSentEvent

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

the class CompressionUtils method main.

public static void main(String[] args) {
    String d = "{\"ip\":\"50.112.119.64\",\"count\":27}$$${\\\"ip\\\":\\\"50.112.119.64\\\",\\\"count\\\":27}";
    String e1 = "{\"ip\":\"11.112.119.64\",\"count\":27}";
    String e2 = "{\"ip\":\"22.111.112.62\",\"count\":27}";
    String e3 = "{\"ip\":\"33.222.112.62\",\"count\":27}";
    List<String> events = new ArrayList<>();
    events.add(e1);
    events.add(e2);
    events.add(e3);
    String encodedString = CompressionUtils.compressAndBase64Encode(events);
    List<MantisServerSentEvent> orig = CompressionUtils.decompressAndBase64Decode(encodedString, true);
    for (MantisServerSentEvent event : orig) {
        System.out.println("event -> " + event);
    }
// String d2 = "blah1$$$blah3$$$blah4";
// System.out.println("pos " + d2.indexOf("$$$"));
// String [] toks = d.split("\\$\\$\\$");
// 
// System.out.println("toks len" + toks.length);
// for(int i=0; i<toks.length; i++) {
// System.out.println("t -> " + toks[i]);
// }
}
Also used : MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) ArrayList(java.util.ArrayList)

Example 3 with MantisServerSentEvent

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

the class ConnectToNamedJob method main.

public static void main(String[] args) {
    // SinkParameters params = new SinkParameters.Builder().withParameter("filter", "windows8").build();
    final AtomicLong eventCounter = new AtomicLong(0L);
    System.setProperty("log4j.logger.io", "DEBUG");
    try {
        Args.parse(ConnectToNamedJob.class, args);
    } catch (IllegalArgumentException e) {
        Args.usage(SubmitEphemeralJob.class);
        System.exit(1);
    }
    Properties properties = new Properties();
    System.out.println("propfile=" + propFile);
    try (InputStream inputStream = new FileInputStream(propFile)) {
        properties.load(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
    final CountDownLatch latch = new CountDownLatch(1);
    MantisSSEJob job = null;
    try {
        job = new MantisSSEJob.Builder(properties).name(jobName).onConnectionReset(new Action1<Throwable>() {

            @Override
            public void call(Throwable throwable) {
                System.err.println("Reconnecting due to error: " + throwable.getMessage());
            }
        }).sinkConnectionsStatusObserver(new Observer<SinkConnectionsStatus>() {

            @Override
            public void onCompleted() {
                System.out.println("ConnectionStatusObserver completed");
            }

            @Override
            public void onError(Throwable e) {
                System.err.println("ConnectionStatusObserver error: " + e.getMessage());
            }

            @Override
            public void onNext(SinkConnectionsStatus status) {
                System.out.println("ConnectionStatusObserver: receiving from " + status.getRecevingDataFrom() + ", connected to " + status.getNumConnected() + " of " + status.getTotal());
            }
        }).sinkDataRecvTimeoutSecs(11).buildJobConnector();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // try{Thread.sleep(3000);}catch(InterruptedException ie){}
    System.out.println("Subscribing now");
    Subscription subscription = job.connectAndGet().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();
                // if(eventCounter.incrementAndGet()>4)
                // throw new RuntimeException("Test exception");
                }
            }).subscribe();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error(throwable.getMessage());
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            System.out.println("Completed");
            System.exit(0);
        }
    }).subscribe();
    // .subscribe();
    try {
        boolean await = latch.await(30, TimeUnit.SECONDS);
        if (await)
            System.out.println("PASSED");
        else
            System.err.println("FAILED!");
        Thread.sleep(5000000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    subscription.unsubscribe();
    System.out.println("Unsubscribed");
    try {
        Thread.sleep(80000);
    } catch (InterruptedException ie) {
    }
    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) IOException(java.io.IOException) Observable(rx.Observable) AtomicLong(java.util.concurrent.atomic.AtomicLong) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) SinkConnectionsStatus(io.mantisrx.client.SinkConnectionsStatus) Observer(rx.Observer) Subscription(rx.Subscription) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 4 with MantisServerSentEvent

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

the class SubmitWithRuntimeLimit method main.

public static void main(String[] args) {
    try {
        Args.parse(SubmitWithRuntimeLimit.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);
    final AtomicBoolean completed = new AtomicBoolean(false);
    final long runtimeLimitSecs = 30;
    MantisSSEJob job = new MantisSSEJob.Builder(properties).name(jobName).jobSla(new JobSla(runtimeLimitSecs, 0L, JobSla.StreamSLAType.Lossy, MantisJobDurationType.Perpetual, "")).onConnectionReset(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            System.err.println("Reconnecting due to error: " + throwable.getMessage());
        }
    }).buildJobSubmitter();
    final Observable<Observable<MantisServerSentEvent>> observable = job.submitAndGet();
    final Subscription subscription = observable.flatMap(new Func1<Observable<MantisServerSentEvent>, Observable<?>>() {

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

                @Override
                public void call(MantisServerSentEvent event) {
                    if (completed.get())
                        System.out.println("FAILURE");
                    System.out.println("Got: " + event.getEventAsString());
                }
            });
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            latch.countDown();
        }
    }).subscribe();
    try {
        // add a buffer for job launch time
        Thread.sleep((runtimeLimitSecs + 10) * 1000);
        // set expectation of complete
        completed.set(true);
        // give some time to see if we still get data, which would be a failure
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    subscription.unsubscribe();
    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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) JobSla(io.mantisrx.runtime.JobSla) Subscription(rx.Subscription) MantisSSEJob(io.mantisrx.client.MantisSSEJob)

Example 5 with MantisServerSentEvent

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

the class SseWorkerConnectionFunctionTest method testSseConnection.

@Test
public void testSseConnection() throws InterruptedException {
    final int serverPort = TestSseServerFactory.getServerPort();
    final CountDownLatch errorLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(1);
    final boolean reconnectOnConnReset = true;
    SseWorkerConnectionFunction connectionFunction = new SseWorkerConnectionFunction(reconnectOnConnReset, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.warn("connection was reset, should be retried", throwable);
            errorLatch.countDown();
        }
    });
    final WorkerConnection<MantisServerSentEvent> conn = connectionFunction.call("localhost", serverPort);
    final Observable<MantisServerSentEvent> events = conn.call();
    events.doOnNext(new Action1<MantisServerSentEvent>() {

        @Override
        public void call(MantisServerSentEvent e) {
            logger.info("got event {}", e.getEventAsString());
            assertEquals(TEST_EVENT_DATA, e.getEventAsString());
            latch.countDown();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error("caught error ", throwable);
            fail("unexpected error");
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            logger.warn("onCompleted");
        }
    }).subscribe();
    errorLatch.await(30, TimeUnit.SECONDS);
    // newServerWithInitialData test server after client started and unable to connect
    // the client should recover and get expected data
    TestSseServerFactory.newServerWithInitialData(serverPort, TEST_EVENT_DATA);
    latch.await(30, TimeUnit.SECONDS);
    TestSseServerFactory.stopAllRunning();
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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