
io.servicetalk.concurrent.internal.TaskBasedSignalOffloader Maven / Gradle / Ivy
/*
* Copyright © 2018-2019 Apple Inc. and the ServiceTalk project authors
*
* 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 io.servicetalk.concurrent.internal;
import io.servicetalk.concurrent.Cancellable;
import io.servicetalk.concurrent.CompletableSource;
import io.servicetalk.concurrent.Executor;
import io.servicetalk.concurrent.PublisherSource.Subscriber;
import io.servicetalk.concurrent.PublisherSource.Subscription;
import io.servicetalk.concurrent.SingleSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import static io.servicetalk.concurrent.Cancellable.IGNORE_CANCEL;
import static io.servicetalk.concurrent.internal.EmptySubscriptions.EMPTY_SUBSCRIPTION;
import static io.servicetalk.concurrent.internal.SubscriberUtils.deliverErrorFromSource;
import static io.servicetalk.concurrent.internal.SubscriberUtils.isRequestNValid;
import static io.servicetalk.concurrent.internal.SubscriberUtils.safeCancel;
import static io.servicetalk.concurrent.internal.SubscriberUtils.safeOnComplete;
import static io.servicetalk.concurrent.internal.SubscriberUtils.safeOnError;
import static io.servicetalk.concurrent.internal.SubscriberUtils.safeOnSuccess;
import static io.servicetalk.utils.internal.PlatformDependent.newUnboundedSpscQueue;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater;
/**
* An implementation of {@link SignalOffloader} that does not hold up a thread for the lifetime of the offloader.
* Instead it enqueues multiple tasks to the provided {@link Consumer executor} and hence is susceptible to not having
* enough capacity in the {@link Consumer executor} when sending signals as compared to detecting insufficient capacity
* earlier as with {@link ThreadBasedSignalOffloader}.
*/
final class TaskBasedSignalOffloader implements SignalOffloader {
private static final Object NULL_WRAPPER = new Object();
private static final Logger LOGGER = LoggerFactory.getLogger(TaskBasedSignalOffloader.class);
private final Executor executor;
private final int publisherSignalQueueInitialCapacity;
TaskBasedSignalOffloader(final Executor executor) {
this(executor, 2);
}
/**
* New instance.
*
* @param executor A {@link Executor} to use for offloading signals.
* @param publisherSignalQueueInitialCapacity Initial capacity for the queue of signals to a {@link Subscriber}.
*/
TaskBasedSignalOffloader(final Executor executor, final int publisherSignalQueueInitialCapacity) {
this.executor = requireNonNull(executor);
this.publisherSignalQueueInitialCapacity = publisherSignalQueueInitialCapacity;
}
@Override
public Subscriber super T> offloadSubscriber(final Subscriber super T> subscriber) {
return new OffloadedSubscriber<>(subscriber, executor, publisherSignalQueueInitialCapacity);
}
@Override
public SingleSource.Subscriber super T> offloadSubscriber(
final SingleSource.Subscriber super T> subscriber) {
return new OffloadedSingleSubscriber<>(executor, subscriber);
}
@Override
public CompletableSource.Subscriber offloadSubscriber(final CompletableSource.Subscriber subscriber) {
return new OffloadedCompletableSubscriber(executor, subscriber);
}
@Override
public Subscriber super T> offloadSubscription(final Subscriber super T> subscriber) {
return new OffloadedSubscriptionSubscriber<>(subscriber, executor);
}
@Override
public SingleSource.Subscriber super T> offloadCancellable(
final SingleSource.Subscriber super T> subscriber) {
return new OffloadedCancellableSingleSubscriber<>(subscriber, executor);
}
@Override
public CompletableSource.Subscriber offloadCancellable(final CompletableSource.Subscriber subscriber) {
return new OffloadedCancellableCompletableSubscriber(subscriber, executor);
}
@Override
public void offloadSubscribe(final Subscriber super T> subscriber,
final Consumer> handleSubscribe) {
try {
executor.execute(() -> handleSubscribe.accept(subscriber));
} catch (Throwable throwable) {
// We assume that if executor accepted the task, it was run and no exception will be thrown from accept.
deliverErrorFromSource(subscriber, throwable);
}
}
@Override
public void offloadSubscribe(final SingleSource.Subscriber super T> subscriber,
final Consumer> handleSubscribe) {
try {
executor.execute(() -> handleSubscribe.accept(subscriber));
} catch (Throwable throwable) {
// We assume that if executor accepted the task, it was run and no exception will be thrown from accept.
deliverErrorFromSource(subscriber, throwable);
}
}
@Override
public void offloadSubscribe(final CompletableSource.Subscriber subscriber,
final Consumer handleSubscribe) {
try {
executor.execute(() -> handleSubscribe.accept(subscriber));
} catch (Throwable throwable) {
// We assume that if executor accepted the task, it was run and no exception will be thrown from accept.
deliverErrorFromSource(subscriber, throwable);
}
}
@Override
public void offloadSignal(final T signal, final Consumer signalConsumer) {
executor.execute(() -> signalConsumer.accept(signal));
}
private static final class OffloadedSubscription implements Subscription, Runnable {
private static final int STATE_IDLE = 0;
private static final int STATE_ENQUEUED = 1;
private static final int STATE_EXECUTING = 2;
public static final int CANCELLED = -1;
public static final int TERMINATED = -2;
private static final AtomicIntegerFieldUpdater stateUpdater =
newUpdater(OffloadedSubscription.class, "state");
private static final AtomicLongFieldUpdater requestedUpdater =
AtomicLongFieldUpdater.newUpdater(OffloadedSubscription.class, "requested");
private final Executor executor;
private final Subscription target;
private volatile int state = STATE_IDLE;
private volatile long requested;
OffloadedSubscription(final Executor executor, final Subscription target) {
this.executor = executor;
this.target = requireNonNull(target);
}
@Override
public void request(final long n) {
if ((!isRequestNValid(n) &&
requestedUpdater.getAndSet(this, n < TERMINATED ? n : Long.MIN_VALUE) >= 0) ||
requestedUpdater.accumulateAndGet(this, n,
FlowControlUtils::addWithOverflowProtectionIfNotNegative) > 0) {
enqueueTaskIfRequired(true);
}
}
@Override
public void cancel() {
long oldVal = requestedUpdater.getAndSet(this, CANCELLED);
if (oldVal != CANCELLED) {
enqueueTaskIfRequired(false);
}
// duplicate cancel.
}
private void enqueueTaskIfRequired(boolean forRequestN) {
final int oldState = stateUpdater.getAndSet(this, STATE_ENQUEUED);
if (oldState == STATE_IDLE) {
try {
executor.execute(this);
} catch (Throwable t) {
// Ideally, we should send an error to the related Subscriber but that would mean we make sure
// we do not concurrently invoke the Subscriber with the original source which would mean we
// add some "lock" in the data path.
// This is an optimistic approach assuming executor rejections are occasional and hence adding
// Subscription -> Subscriber dependency for all paths is too costly.
// As we do for other cases, we simply invoke the target in the calling thread.
if (forRequestN) {
LOGGER.error("Failed to execute task on the executor {}. " +
"Invoking Subscription (request()) in the caller thread. Subscription {}.",
executor, target, t);
target.request(requestedUpdater.getAndSet(this, 0));
} else {
requested = TERMINATED;
LOGGER.error("Failed to execute task on the executor {}. " +
"Invoking Subscription (cancel()) in the caller thread. Subscription {}.",
executor, target, t);
target.cancel();
}
// We swallow the error here as we are forwarding the actual call and throwing from here will
// interrupt the control flow.
}
}
}
@Override
public void run() {
state = STATE_EXECUTING;
for (;;) {
long r = requestedUpdater.getAndSet(this, 0);
if (r > 0) {
try {
target.request(r);
continue;
} catch (Throwable t) {
// Cancel since request-n threw.
requested = r = CANCELLED;
LOGGER.error("Unexpected exception from request(). Subscription {}.", target, t);
}
}
if (r == CANCELLED) {
requested = TERMINATED;
safeCancel(target);
return; // No more signals are required to be sent.
} else if (r == TERMINATED) {
return; // we want to hard return to avoid resetting state.
} else if (r != 0) {
// Invalid request-n
//
// As per spec (Rule 3.9) a request-n with n <= 0 MUST signal an onError hence terminating the
// Subscription. Since, we can not store negative values in requested and keep going without
// requesting more invalid values, we assume spec compliance (no more data can be requested) and
// terminate.
requested = TERMINATED;
try {
target.request(r);
} catch (IllegalArgumentException iae) {
// Expected
} catch (Throwable t) {
LOGGER.error("Ignoring unexpected exception from request(). Subscription {}.", target, t);
}
return;
}
// We store a request(0) as Long.MIN_VALUE so if we see r == 0 here, it means we are re-entering
// the loop because we saw the STATE_ENQUEUED but we have already read from requested.
for (;;) {
final int cState = state;
if (cState == STATE_EXECUTING) {
if (stateUpdater.compareAndSet(this, STATE_EXECUTING, STATE_IDLE)) {
return;
}
} else if (cState == STATE_ENQUEUED) {
if (stateUpdater.compareAndSet(this, STATE_ENQUEUED, STATE_EXECUTING)) {
break;
}
} else {
return;
}
}
}
}
}
private static final class OffloadedSubscriber implements Subscriber, Runnable {
private static final int STATE_IDLE = 0;
private static final int STATE_ENQUEUED = 1;
private static final int STATE_EXECUTING = 2;
private static final int STATE_TERMINATING = 3;
private static final int STATE_TERMINATED = 4;
@SuppressWarnings("rawtypes")
private static final AtomicIntegerFieldUpdater stateUpdater =
newUpdater(OffloadedSubscriber.class, "state");
private volatile int state = STATE_IDLE;
private final Subscriber super T> target;
private final Executor executor;
private final Queue
© 2015 - 2025 Weber Informatics LLC | Privacy Policy