Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions core/trino-main/src/main/java/io/trino/operator/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.base.Verify.verify;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static io.airlift.concurrent.MoreFutures.getFutureValue;
import static io.trino.operator.Operator.NOT_BLOCKED;
Expand Down Expand Up @@ -698,22 +699,21 @@ private <T> Optional<T> tryWithLock(long timeout, TimeUnit unit, boolean interru
return Optional.empty();
}

Optional<T> result;
T result = null;
Throwable failure = null;

try {
result = Optional.of(task.get());
result = task.get();

// opportunistic check to avoid unnecessary lock reacquisition
processNewSources();
destroyIfNecessary();
}
catch (Throwable t) {
failure = t;
}
finally {
try {
try {
processNewSources();
}
finally {
destroyIfNecessary();
}
}
finally {
exclusiveLock.unlock();
}
exclusiveLock.unlock();
}

// If there are more assignment updates available, attempt to reacquire the lock and process them.
Expand All @@ -727,16 +727,40 @@ private <T> Optional<T> tryWithLock(long timeout, TimeUnit unit, boolean interru
try {
processNewSources();
}
finally {
catch (Throwable t) {
if (failure == null) {
failure = t;
}
else if (failure != t) {
failure.addSuppressed(t);
}
}

try {
destroyIfNecessary();
}
catch (Throwable t) {
if (failure == null) {
failure = t;
}
else if (failure != t) {
failure.addSuppressed(t);
}
}
}
finally {
exclusiveLock.unlock();
}
}

return result;
if (failure != null) {
throwIfUnchecked(failure);
// should never happen
throw new AssertionError(failure);
}

verify(result != null, "result is null");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary. The Optional.of() construction will fail if it's null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this more as a documentation so the intention is clearly stated. Otherwise when some task starts returning null somebody may interpret the Optional.of as a mistake and switch it to Optional.ofNullable instead of fixing the actual problem.

return Optional.of(result);
}

private static class DriverLock
Expand Down