Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for #1102 Detect some blocking APIs inside parallel and single Scheduler #1109

Merged
merged 9 commits into from
Mar 8, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.reactivestreams.Subscription;
import reactor.core.Disposable;
import reactor.core.Exceptions;
import reactor.core.scheduler.Schedulers;
import reactor.util.annotation.Nullable;

/**
Expand Down Expand Up @@ -109,6 +110,9 @@ public final void dispose() {
* @return an Optional representing the first value (or empty Optional if the source is empty)
*/
final Optional<T> blockingGet() {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("blockOptional() is blocking, which is not supported in thread " + Thread.currentThread().getName());
}
if (getCount() != 0) {
try {
await();
Expand Down Expand Up @@ -142,6 +146,9 @@ final Optional<T> blockingGet() {
* @return an Optional representing the first value (or empty Optional if the source is empty)
*/
final Optional<T> blockingGet(long timeout, TimeUnit unit) {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("blockOptional() is blocking, which is not supported in thread " + Thread.currentThread().getName());
}
if (getCount() != 0) {
try {
if (!await(timeout, unit)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.reactivestreams.Subscription;
import reactor.core.Disposable;
import reactor.core.Exceptions;
import reactor.core.scheduler.Schedulers;
import reactor.util.annotation.Nullable;

/**
Expand Down Expand Up @@ -72,6 +73,9 @@ public final void dispose() {
*/
@Nullable
final T blockingGet() {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("block()/blockFirst()/blockLast() are blocking, which is not supported in thread " + Thread.currentThread().getName());
}
if (getCount() != 0) {
try {
await();
Expand Down Expand Up @@ -103,6 +107,9 @@ final T blockingGet() {
*/
@Nullable
final T blockingGet(long timeout, TimeUnit unit) {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("block()/blockFirst()/blockLast() are blocking, which is not supported in thread " + Thread.currentThread().getName());
}
if (getCount() != 0) {
try {
if (!await(timeout, unit)) {
Expand Down
6 changes: 6 additions & 0 deletions reactor-core/src/main/java/reactor/core/publisher/Flux.java
Original file line number Diff line number Diff line change
Expand Up @@ -7733,6 +7733,9 @@ public final Iterable<T> toIterable(int batchSize) {
*/
public final Iterable<T> toIterable(int batchSize, @Nullable Supplier<Queue<T>>
queueProvider) {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("toIterable() is blocking, which is not supported in thread " + Thread.currentThread().getName());
}
final Supplier<Queue<T>> provider;
if(queueProvider == null){
provider = Queues.get(batchSize);
Expand Down Expand Up @@ -7768,6 +7771,9 @@ public final Stream<T> toStream() {
* @return a {@link Stream} of unknown size with onClose attached to {@link Subscription#cancel()}
*/
public final Stream<T> toStream(int batchSize) {
if (!Schedulers.isBlockingCurrentThreadOk()) {
throw new UnsupportedOperationException("toStream() is blocking, which is not supported in thread " + Thread.currentThread().getName());
}
final Supplier<Queue<T>> provider;
provider = Queues.get(batchSize);
return new BlockingIterable<>(this, batchSize, provider).stream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public Disposable schedulePeriodically(Runnable task, long initialDelay, long pe
public String toString() {
StringBuilder ts = new StringBuilder(Schedulers.ELASTIC)
.append('(');
if (factory instanceof Schedulers.SchedulerThreadFactory) {
ts.append('\"').append(((Schedulers.SchedulerThreadFactory) factory).get()).append('\"');
if (factory instanceof ReactorThreadFactory) {
ts.append('\"').append(((ReactorThreadFactory) factory).get()).append('\"');
}
ts.append(')');
return ts.toString();
Expand Down
34 changes: 34 additions & 0 deletions reactor-core/src/main/java/reactor/core/scheduler/NonBlocking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package reactor.core.scheduler;

import java.util.concurrent.ThreadFactory;

/**
* A marker interface that is detected on {@link Thread Threads} while executing Reactor
* blocking APIs, resulting in these calls throwing an exception.
* <p>
* Extend {@link AbstractReactorThreadFactory} for a {@link ThreadFactory} that can easily
* create such threads, and optionally name them and further configure them.
* <p>
* See {@link Schedulers#isBlockingCurrentThreadOk()} and
* {@link Schedulers#isBlockingCurrentThreadOk(Thread)} for a check that includes detecting
* this marker interface.
*
* @author Simon Baslé
*/
public interface NonBlocking { }
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public Disposable schedulePeriodically(Runnable task,
public String toString() {
StringBuilder ts = new StringBuilder(Schedulers.PARALLEL)
.append('(').append(n);
if (factory instanceof Schedulers.SchedulerThreadFactory) {
ts.append(",\"").append(((Schedulers.SchedulerThreadFactory) factory).get()).append('\"');
if (factory instanceof ReactorThreadFactory) {
ts.append(",\"").append(((ReactorThreadFactory) factory).get()).append('\"');
}
ts.append(')');
return ts.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package reactor.core.scheduler;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import reactor.util.annotation.Nullable;

/**
* The standard Reactor {@link ThreadFactory Thread factories} to be used by {@link Scheduler},
* creating {@link Thread} with a prefix (which can be retrieved with the {@link #get()} method).
*
* @author Simon Baslé
*/
class ReactorThreadFactory implements ThreadFactory,
Supplier<String>,
Thread.UncaughtExceptionHandler {

final private String name;
final private AtomicLong counterReference;
final private boolean daemon;
final private boolean rejectBlocking;

@Nullable
final private BiConsumer<Thread, Throwable> uncaughtExceptionHandler;

ReactorThreadFactory(String name,
AtomicLong counterReference,
boolean daemon,
boolean rejectBlocking,
@Nullable BiConsumer<Thread, Throwable> uncaughtExceptionHandler) {
this.name = name;
this.counterReference = counterReference;
this.daemon = daemon;
this.rejectBlocking = rejectBlocking;
this.uncaughtExceptionHandler = uncaughtExceptionHandler;
}

@Override
public final Thread newThread(@NotNull Runnable runnable) {
String newThreadName = name + "-" + counterReference.incrementAndGet();
Thread t = rejectBlocking
? new NonBlockingThread(runnable, newThreadName)
: new Thread(runnable, newThreadName);
if (daemon) {
t.setDaemon(true);
}
if (uncaughtExceptionHandler != null) {
t.setUncaughtExceptionHandler(this);
}
return t;
}

@Override
public void uncaughtException(Thread t, Throwable e) {
if (uncaughtExceptionHandler == null) {
return;
}

uncaughtExceptionHandler.accept(t,e);
}

/**
* Get the prefix used for new {@link Thread Threads} created by this {@link ThreadFactory}.
* The factory can also be seen as a {@link Supplier Supplier&lt;String&gt;}.
*
* @return the thread name prefix
*/
@Override
public final String get() {
return name;
}

static final class NonBlockingThread extends Thread implements NonBlocking {

public NonBlockingThread(Runnable target, String name) {
super(target, name);
}
}
}
Loading