Java Code Examples for reactor.core.scheduler.Schedulers#parallel()

The following examples show how to use reactor.core.scheduler.Schedulers#parallel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultOneOffReconcilerTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private void newReconciler(Function<String, List<Mono<Function<String, String>>>> reconcilerActionsProvider) {
    this.reconciler = new DefaultOneOffReconciler<>(
            "junit",
            INITIAL,
            QUICK_CYCLE,
            LONG_CYCLE,
            reconcilerActionsProvider,
            Schedulers.parallel(),
            titusRuntime
    );

    this.changesSubscriber = new TitusRxSubscriber<>();
    reconciler.changes().subscribe(changesSubscriber);

    String event = changesSubscriber.takeNext();
    if (!event.equals(INITIAL) && !event.startsWith(RECONCILED)) {
        fail("Unexpected event: " + event);
    }
}
 
Example 2
Source File: JobDataReplicatorProvider.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private static RetryableReplicatorEventStream<JobSnapshot, JobManagerEvent<?>> newReplicatorEventStream(JobManagementClient client,
                                                                                                        Map<String, String> filteringCriteria,
                                                                                                        TitusRuntime titusRuntime) {
    GrpcJobReplicatorEventStream grpcEventStream = new GrpcJobReplicatorEventStream(
            client,
            filteringCriteria,
            new JobDataReplicatorMetrics(JOB_REPLICATOR_GRPC_STREAM, titusRuntime),
            titusRuntime,
            Schedulers.parallel()
    );
    return new RetryableReplicatorEventStream<>(
            grpcEventStream,
            new JobDataReplicatorMetrics(JOB_REPLICATOR_RETRYABLE_STREAM, titusRuntime),
            titusRuntime,
            Schedulers.parallel()
    );
}
 
Example 3
Source File: FluxProcessorTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubmitSession() throws Exception {
	FluxIdentityProcessor<Integer> processor = EmitterProcessor.create();
	AtomicInteger count = new AtomicInteger();
	CountDownLatch latch = new CountDownLatch(1);
	Scheduler scheduler = Schedulers.parallel();
	processor.publishOn(scheduler)
	         .delaySubscription(Duration.ofMillis(1000))
	         .limitRate(1)
	         .subscribe(d -> {
		         count.incrementAndGet();
		         latch.countDown();
	         });

	FluxSink<Integer> session = processor.sink();
	session.next(1);
	//System.out.println(emission);
	session.complete();

	latch.await(5, TimeUnit.SECONDS);
	Assert.assertTrue("latch : " + count, count.get() == 1);
	scheduler.dispose();
}
 
Example 4
Source File: HealthLocalMasterReadinessResolverTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    when(healthcheck.check()).thenAnswer(invocation -> {
        CompletableFuture<HealthCheckStatus> future = new CompletableFuture<>();
        if (simulatedError != null) {
            future.completeExceptionally(new RuntimeException("simulated error"));
        } else if (currentHealthStatus != null) {
            future.complete(currentHealthStatus);
        } // else never complete
        invocationCounter++;
        return future;
    });

    this.resolver = new HealthLocalMasterReadinessResolver(
            healthcheck,
            REFRESH_SCHEDULER_DESCRIPTOR.toBuilder()
                    .withInterval(Duration.ofMillis(1))
                    .withTimeout(Duration.ofMillis(1))
                    .build(),
            titusRuntime,
            Schedulers.parallel()
    );
}
 
Example 5
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void coordinatorReachableThroughCacheInnerSubscriptionsOnly() throws InterruptedException {
	TestPublisher<Integer> source = TestPublisher.create();

	MonoCacheTime<Integer> cached = new MonoCacheTime<>(source.mono(),
			Duration.ofMillis(100), //short cache TTL should trigger state change if source is not never
			Schedulers.parallel());

	Disposable d1 = cached.subscribe();
	cached.subscribe();

	WeakReference<Signal<Integer>> refCoordinator = new WeakReference<>(cached.state);

	assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);

	Thread.sleep(150);
	source = null;
	cached = null;
	System.gc();

	assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
}
 
Example 6
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheDependingOnSignal() throws InterruptedException {
	AtomicInteger count = new AtomicInteger();

	Mono<Integer> source = Mono.fromCallable(count::incrementAndGet);

	Mono<Integer> cached = new MonoCacheTime<>(source,
			sig -> {
				if (sig.isOnNext()) {
					return Duration.ofMillis(100 * sig.get());
				}
				return Duration.ZERO;
			}, Schedulers.parallel());

	cached.block();
	cached.block();
	assertThat(cached.block())
			.as("after 110ms")
			.isEqualTo(1);

	Thread.sleep(110);
	cached.block();
	assertThat(cached.block())
			.as("after 220ms")
			.isEqualTo(2);

	Thread.sleep(110);
	assertThat(cached.block())
			.as("after 330ms")
			.isEqualTo(2);

	Thread.sleep(110);
	cached.block();
	assertThat(cached.block())
			.as("after 440ms")
			.isEqualTo(3);

	assertThat(count).hasValue(3);
}
 
Example 7
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void nextTtlGeneratorTransientFailure() {
	AtomicInteger count = new AtomicInteger();

	Mono<Integer> cached = new MonoCacheTime<>(Mono.fromCallable(count::incrementAndGet),
			v -> {
				if (v == 1) throw new IllegalStateException("transient");
				return Duration.ofMillis(200 * v);
			},
			t -> Duration.ofSeconds(10),
			() -> Duration.ofSeconds(10),
			Schedulers.parallel());

	StepVerifier.create(cached)
	            .expectErrorSatisfies(e -> assertThat(e)
			            .isInstanceOf(IllegalStateException.class)
			            .hasMessage("transient")
			            .hasNoSuppressedExceptions())
	            .verify();

	StepVerifier.create(cached)
	            .expectNext(2)
	            .expectComplete()
	            .verify();

	assertThat(cached.block())
			.as("cached after cache miss")
			.isEqualTo(2);

	assertThat(count).as("source invocations")
	                 .hasValue(2);
}
 
Example 8
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void errorTtlGeneratorTransientFailureCheckHooks() {
	Throwable exception = new IllegalArgumentException("foo");
	AtomicInteger count = new AtomicInteger();

	Mono<Integer> cached = new MonoCacheTime<>(Mono.error(exception),
			v -> Duration.ofSeconds(10),
			t -> {
				if (count.incrementAndGet() == 1) throw new IllegalStateException("transient");
				return Duration.ofMillis(100);
			},
			() -> Duration.ofSeconds(10),
			Schedulers.parallel());

	StepVerifier.create(cached)
	            .expectErrorSatisfies(e -> assertThat(e)
			            .isInstanceOf(IllegalStateException.class)
			            .hasMessage("transient")
			            .hasSuppressedException(exception))
	            .verifyThenAssertThat()
	            .hasNotDroppedElements()
	            .hasNotDroppedErrors();

	StepVerifier.create(cached)
	            .expectErrorSatisfies(e -> assertThat(e)
			            .isInstanceOf(IllegalArgumentException.class)
			            .hasMessage("foo"))
	            .verifyThenAssertThat()
	            .hasNotDroppedErrors()
	            .hasNotDroppedElements();

	assertThatExceptionOfType(IllegalArgumentException.class)
			.as("cached after cache miss")
			.isThrownBy(cached::block);

	assertThat(count).as("source invocations")
	                 .hasValue(2);
}
 
Example 9
Source File: SingleClusterMemberResolverTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
    serviceStub.addMember(MEMBER_1.toBuilder()
            .setCurrent(MEMBER_1.getCurrent().toBuilder()
                    .setLeadershipState(ClusterMember.LeadershipState.Leader)
            )
            .build()
    );
    serviceStub.addMember(MEMBER_2);
    serviceStub.addMember(MEMBER_3);

    String serviceName = "clusterMembershipService#" + System.currentTimeMillis();
    this.server = InProcessServerBuilder.forName(serviceName)
            .directExecutor()
            .addService(serviceStub)
            .build()
            .start();

    this.resolver = new SingleClusterMemberResolver(
            configuration,
            address -> InProcessChannelBuilder.forName(serviceName).directExecutor().build(),
            ADDRESS,
            Schedulers.parallel(),
            titusRuntime
    );

    // Starts with a healthy connection.
    await().until(() -> resolver.getPrintableName().equals(MEMBER_1.getCurrent().getMemberId()));
}
 
Example 10
Source File: RetryableReplicatorEventStreamTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private RetryableReplicatorEventStream<String, String> newStream() {
    when(delegate.connect()).thenAnswer(invocation -> Flux.defer(() -> {
        eventSubject = EmitterProcessor.create();
        return eventSubject;
    }));

    return new RetryableReplicatorEventStream<>(
            delegate, new DataReplicatorMetrics("test", titusRuntime), titusRuntime, Schedulers.parallel()
    );
}
 
Example 11
Source File: DefaultRelocationWorkflowExecutor.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Inject
public DefaultRelocationWorkflowExecutor(RelocationConfiguration configuration,
                                         AgentDataReplicator agentDataReplicator,
                                         ReadOnlyAgentOperations agentOperations,
                                         JobDataReplicator jobDataReplicator,
                                         ReadOnlyJobOperations jobOperations,
                                         EvictionDataReplicator evictionDataReplicator,
                                         EvictionServiceClient evictionServiceClient,
                                         DeschedulerService deschedulerService,
                                         TaskRelocationStore activeStore,
                                         TaskRelocationResultStore archiveStore,
                                         TitusRuntime titusRuntime) {
    this.configuration = configuration;
    this.agentDataReplicator = agentDataReplicator;
    this.jobDataReplicator = jobDataReplicator;
    this.evictionDataReplicator = evictionDataReplicator;
    this.metrics = new WorkflowMetrics(titusRuntime);
    this.titusRuntime = titusRuntime;

    newRelocationPlanEmitter.onNext(Collections.emptyList());

    ensureReplicatorsReady();

    RelocationTransactionLogger transactionLog = new RelocationTransactionLogger(jobOperations);
    this.relocationMetricsStep = new RelocationMetricsStep(agentOperations, jobOperations, titusRuntime);
    this.mustBeRelocatedSelfManagedTaskCollectorStep = new MustBeRelocatedSelfManagedTaskCollectorStep(agentOperations, jobOperations, titusRuntime);
    this.mustBeRelocatedTaskStoreUpdateStep = new MustBeRelocatedTaskStoreUpdateStep(configuration, activeStore, transactionLog, titusRuntime);
    this.deschedulerStep = new DeschedulerStep(deschedulerService, transactionLog, titusRuntime);
    this.taskEvictionStep = new TaskEvictionStep(evictionServiceClient, titusRuntime, transactionLog, Schedulers.parallel());
    this.taskEvictionResultStoreStep = new TaskEvictionResultStoreStep(configuration, archiveStore, transactionLog, titusRuntime);
    this.lastDeschedulingTimestamp = titusRuntime.getClock().wallTime();
    this.deschedulingResultLogger = new DeschedulingResultLogger();
}
 
Example 12
Source File: TaskTerminationExecutorTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private TaskTerminationExecutor newTerminationExecutor() {
    TaskTerminationExecutor executor = new TaskTerminationExecutor(
            configuration,
            jobComponentStub.getJobOperations(),
            quotasManager,
            titusRuntime,
            Schedulers.parallel()
    );
    executor.events().subscribe(eventSubscriber);
    return executor;
}
 
Example 13
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyTtlGeneratorTransientFailure() {
	AtomicInteger count = new AtomicInteger();

	Mono<Integer> cached = new MonoCacheTime<>(Mono.empty(),
			v -> Duration.ofSeconds(10),
			t -> Duration.ofSeconds(10),
			() -> {
				if (count.incrementAndGet() == 1) throw new IllegalStateException("transient");
				return Duration.ofMillis(100);
			},
			Schedulers.parallel());

	StepVerifier.create(cached)
	            .expectErrorSatisfies(e -> assertThat(e)
			            .isInstanceOf(IllegalStateException.class)
			            .hasMessage("transient")
			            .hasNoSuppressedExceptions())
	            .verify();

	StepVerifier.create(cached)
	            .expectComplete()
	            .verify();

	assertThat(cached.block())
			.as("cached after cache miss")
			.isNull();

	assertThat(count).as("source invocations")
	                 .hasValue(2);
}
 
Example 14
Source File: GrpcEvictionReplicatorEventStreamTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private GrpcEvictionReplicatorEventStream newStream() {
    return new GrpcEvictionReplicatorEventStream(client, new DataReplicatorMetrics("test", titusRuntime), titusRuntime, Schedulers.parallel());
}
 
Example 15
Source File: MonoCacheTimeTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void cacheDependingOnValueAndError() throws InterruptedException {
	AtomicInteger count = new AtomicInteger();

	Mono<Integer> source = Mono.fromCallable(count::incrementAndGet)
			.map(i -> {
				if (i == 2) throw new IllegalStateException("transient boom");
				return i;
			});

	AtomicInteger onNextTtl = new AtomicInteger();
	AtomicInteger onErrorTtl = new AtomicInteger();
	AtomicInteger onEmptyTtl = new AtomicInteger();

	Mono<Integer> cached = new MonoCacheTime<>(source,
			v -> { onNextTtl.incrementAndGet(); return Duration.ofMillis(100 * v);},
			e -> { onErrorTtl.incrementAndGet(); return Duration.ofMillis(300);},
			() -> { onEmptyTtl.incrementAndGet(); return Duration.ZERO;} ,
			Schedulers.parallel());

	cached.block();
	cached.block();
	assertThat(cached.block())
			.as("1")
			.isEqualTo(1);

	Thread.sleep(110);
	assertThatExceptionOfType(IllegalStateException.class)
			.as("2 errors")
			.isThrownBy(cached::block);

	Thread.sleep(210);
	assertThatExceptionOfType(IllegalStateException.class)
			.as("2 still errors")
			.isThrownBy(cached::block);
	assertThat(count).as("2 is cached").hasValue(2);

	Thread.sleep(110);
	assertThat(cached.block())
			.as("3 emits again")
			.isEqualTo(3);

	Thread.sleep(210);
	assertThat(cached.block())
			.as("3 is cached")
			.isEqualTo(3);

	assertThat(count).hasValue(3);
	assertThat(onNextTtl).as("onNext TTL generations").hasValue(2);
	assertThat(onErrorTtl).as("onError TTL generations").hasValue(1);
	assertThat(onEmptyTtl).as("onEmpty TTL generations").hasValue(0);
}
 
Example 16
Source File: MonoDelayElementTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
private Scheduler defaultSchedulerForDelay() {
	return Schedulers.parallel(); //reflects the default used in Mono.delay(duration)
}
 
Example 17
Source File: HealthLocalMasterReadinessResolver.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Inject
public HealthLocalMasterReadinessResolver(HealthCheckAggregator healthCheckAggregator,
                                          TitusRuntime titusRuntime) {
    this(healthCheckAggregator, REFRESH_SCHEDULER_DESCRIPTOR, titusRuntime, Schedulers.parallel());
}
 
Example 18
Source File: GrpcJobReplicatorEventStreamTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private GrpcJobReplicatorEventStream newStream() {
    when(client.observeJobs(any())).thenReturn(ReactorExt.toFlux(dataGenerator.observeJobs(true)));
    return new GrpcJobReplicatorEventStream(client, new DataReplicatorMetrics("test", titusRuntime), titusRuntime, Schedulers.parallel());
}
 
Example 19
Source File: GrpcAgentReplicatorEventStreamTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private GrpcAgentReplicatorEventStream newStream() {
    when(client.observeAgents()).thenReturn(agentComponentStub.grpcObserveAgents(true));
    return new GrpcAgentReplicatorEventStream(client, new DataReplicatorMetrics("test", titusRuntime), titusRuntime, Schedulers.parallel());
}
 
Example 20
Source File: TaskEvictionStepTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    this.step = new TaskEvictionStep(evictionServiceClient, titusRuntime, transactionLog, Schedulers.parallel());
}