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