use of rx.internal.util.RxThreadFactory in project mantis by Netflix.
the class MantisRxSingleThreadSchedulerTest method testObserveOnAfterOnCompleteMantisRxScheduler.
@Test
public void testObserveOnAfterOnCompleteMantisRxScheduler() throws InterruptedException {
int nThreads = 6;
final MantisRxSingleThreadScheduler[] mantisRxSingleThreadSchedulers = new MantisRxSingleThreadScheduler[nThreads];
RxThreadFactory rxThreadFactory = new RxThreadFactory("MantisRxScheduler-");
logger.info("creating {} Mantis threads", nThreads);
for (int i = 0; i < nThreads; i++) {
mantisRxSingleThreadSchedulers[i] = new MantisRxSingleThreadScheduler(rxThreadFactory);
}
int numInnerObs = 10;
int valuesPerCompletingInnerObs = 2;
int valuesToWaitFor = numInnerObs * valuesPerCompletingInnerObs + 10;
Observable<Observable<String>> oo = createSourceObs(numInnerObs, valuesPerCompletingInnerObs);
final CountDownLatch latch = new CountDownLatch(valuesToWaitFor);
Observable<String> map = oo.lift(new MonitorOperator<>("worker_stage_outer")).map(observable -> observable.groupBy(e -> System.nanoTime() % nThreads).flatMap(go -> go.observeOn(mantisRxSingleThreadSchedulers[go.getKey().intValue()]).doOnNext(x -> {
logger.info("processing {} on thread {}", x, Thread.currentThread().getName());
latch.countDown();
}))).flatMap(x -> x);
Subscription subscription = map.subscribe();
assertTrue(latch.await(5, TimeUnit.SECONDS));
for (int i = 0; i < nThreads; i++) {
assertFalse(mantisRxSingleThreadSchedulers[i].createWorker().isUnsubscribed());
}
subscription.unsubscribe();
}
use of rx.internal.util.RxThreadFactory in project mantis by Netflix.
the class StageExecutors method executeMantisGroupsInParallel.
/**
* @param go
* @param computation
*
* @return untyped to support multiple callers return types
*/
@SuppressWarnings("unchecked")
private static <K, T, R> Observable<Observable<R>> executeMantisGroupsInParallel(Observable<Observable<MantisGroup<K, T>>> go, Computation computation, final Context context, final boolean applyTimeoutToInners, final long timeout, final int concurrency) {
logger.info("initializing {}", computation.getClass().getCanonicalName());
computation.init(context);
// from groups to observable
final Func2<Context, Observable<MantisGroup<K, T>>, Observable<R>> c = (Func2<Context, Observable<MantisGroup<K, T>>, Observable<R>>) computation;
if (concurrency == StageConfig.DEFAULT_STAGE_CONCURRENCY) {
return go.lift(new MonitorOperator<>("worker_stage_outer")).map(observable -> c.call(context, observable.observeOn(Schedulers.computation()).lift(new MonitorOperator<>("worker_stage_inner_input"))).lift(new MonitorOperator<>("worker_stage_inner_output")));
} else {
final MantisRxSingleThreadScheduler[] mantisRxSingleThreadSchedulers = new MantisRxSingleThreadScheduler[concurrency];
RxThreadFactory rxThreadFactory = new RxThreadFactory("MantisRxSingleThreadScheduler-");
logger.info("creating {} Mantis threads", concurrency);
for (int i = 0; i < concurrency; i++) {
mantisRxSingleThreadSchedulers[i] = new MantisRxSingleThreadScheduler(rxThreadFactory);
}
return go.lift(new MonitorOperator<>("worker_stage_outer")).map(observable -> observable.groupBy(e -> Math.abs(e.getKeyValue().hashCode()) % concurrency).flatMap(gbo -> c.call(context, gbo.observeOn(mantisRxSingleThreadSchedulers[gbo.getKey().intValue()]).lift(new MonitorOperator<MantisGroup<K, T>>("worker_stage_inner_input"))).lift(new MonitorOperator<R>("worker_stage_inner_output"))));
}
}
use of rx.internal.util.RxThreadFactory in project mantis by Netflix.
the class StageExecutors method executeInnersInParallel.
/**
* @param oo
* @param computation
*
* @return untyped to support multiple callers return types
*/
@SuppressWarnings("unchecked")
private static <T, R> Observable<Observable<R>> executeInnersInParallel(Observable<Observable<T>> oo, Computation computation, final Context context, final boolean applyTimeoutToInners, final long timeout, final int concurrency) {
logger.info("initializing {}", computation.getClass().getCanonicalName());
computation.init(context);
// from groups to observable
final Func2<Context, Observable<T>, Observable<R>> c = (Func2<Context, Observable<T>, Observable<R>>) computation;
if (concurrency == StageConfig.DEFAULT_STAGE_CONCURRENCY) {
return oo.lift(new MonitorOperator<>("worker_stage_outer")).map(observable -> c.call(context, observable.observeOn(Schedulers.computation()).lift(new MonitorOperator<T>("worker_stage_inner_input"))).lift(new MonitorOperator<R>("worker_stage_inner_output")));
} else {
final MantisRxSingleThreadScheduler[] mantisRxSingleThreadSchedulers = new MantisRxSingleThreadScheduler[concurrency];
RxThreadFactory rxThreadFactory = new RxThreadFactory("MantisRxSingleThreadScheduler-");
logger.info("creating {} Mantis threads", concurrency);
for (int i = 0; i < concurrency; i++) {
mantisRxSingleThreadSchedulers[i] = new MantisRxSingleThreadScheduler(rxThreadFactory);
}
return oo.lift(new MonitorOperator<>("worker_stage_outer")).map(observable -> observable.groupBy(e -> System.nanoTime() % concurrency).flatMap(go -> c.call(context, go.observeOn(mantisRxSingleThreadSchedulers[go.getKey().intValue()]).lift(new MonitorOperator<>("worker_stage_inner_input"))).lift(new MonitorOperator<>("worker_stage_inner_output"))));
}
}
Aggregations