Skip to content

Do not interrupt underlying Azure repository threads during errors #99320

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions docs/changelog/99320.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 99320
summary: Do not interrupt underlying Azure repository threads during errors
area: Snapshot/Restore
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.elasticsearch.core.Strings.format;

Expand Down Expand Up @@ -122,6 +125,16 @@ public void execute(Runnable command) {
delegate.execute(decorateRunnable(command));
}

@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new UninterruptibleFuture<>(super.newTaskFor(runnable, value));
}

@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new UninterruptibleFuture<>(super.newTaskFor(callable));
}
Comment on lines +129 to +136
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these creating a new task for each read operation? Just thinking about how promptly a read might be cancelled without the interrupt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, see org.elasticsearch.repositories.azure.AzureBlobStore#convertStreamToByteBuffer. We're reading from the input stream in 64Kb chunks, and we emit those in order; meaning that after the pipeline has been cancelled at most we'll need to wait until we're able to read 64Kb from disk.


protected Runnable decorateRunnable(Runnable command) {
return command;
}
Expand Down Expand Up @@ -172,4 +185,45 @@ public V get(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}
}

@SuppressForbidden(reason = "It wraps a Future to avoid interrupting threads")
private static final class UninterruptibleFuture<V> implements RunnableFuture<V> {
private final RunnableFuture<V> delegate;

UninterruptibleFuture(RunnableFuture<V> delegate) {
this.delegate = delegate;
}

@Override
public void run() {
delegate.run();
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
// Ensure that the thread is never interrupted
return delegate.cancel(false);
}

@Override
public boolean isCancelled() {
return delegate.isCancelled();
}

@Override
public boolean isDone() {
return delegate.isDone();
}

@Override
public V get() throws InterruptedException, ExecutionException {
return delegate.get();
}

@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return delegate.get(timeout, unit);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import fixture.azure.AzureHttpHandler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -37,6 +38,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -472,4 +474,32 @@ public void testRetryFromSecondaryLocationPolicies() throws Exception {
assertThat(failedGetCalls.get(), equalTo(1));
}
}

public void testPrematureClosedConnectionDoesNotInterruptBackingThread() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please point on the place where you simulate thread.interrupt()? is it missed or interruption is implicit somehow?

Copy link
Contributor

Choose a reason for hiding this comment

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

The interrupt comes from the future being cancelled due to the premature connection close set up in the httpServer.createContext line - if you undo the fix (e.g. apply the following patch) then the test fails:

diff --git a/modules/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java b/modules/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java
index f621cfe3e979..4c2c378acb12 100644
--- a/modules/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java
+++ b/modules/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java
@@ -202,7 +202,7 @@ public class ReactorScheduledExecutorService extends AbstractExecutorService imp
         @Override
         public boolean cancel(boolean mayInterruptIfRunning) {
             // Ensure that the thread is never interrupted
-            return delegate.cancel(false);
+            return delegate.cancel(mayInterruptIfRunning);
         }

         @Override

final int maxRetries = 0;

final byte[] data = randomBytes(ByteSizeUnit.KB.toIntBytes(512));

httpServer.createContext("/account/container/closed_connection_blob", HttpExchange::close);

final BlobContainer blobContainer = createBlobContainer(maxRetries);

var interruptedThread = new AtomicBoolean();
try (InputStream stream = new InputStreamIndexInput(new ByteArrayIndexInput("desc", data), data.length) {
@Override
public int read(byte[] b, int off, int len) throws IOException {
try {
// Ensure that the thread where the stream is read is not interrupted
Thread.sleep(250);
} catch (InterruptedException e) {
interruptedThread.set(true);
}
return super.read(b, off, len);
}
}) {
expectThrows(IOException.class, () -> blobContainer.writeBlob("closed_connection_blob", stream, data.length, false));
assertFalse(interruptedThread.get());
}
}

}