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");
}
}
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]);
// }
}
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);
}
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);
}
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();
}
Aggregations