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

Make reindex wait for cleanup before responding #23677

Merged
merged 2 commits into from
Mar 21, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,18 @@ protected void finishHim(Exception failure) {
* @param searchFailures any search failures accumulated during the request
* @param timedOut have any of the sub-requests timed out?
*/
protected void finishHim(Exception failure, List<Failure> indexingFailures, List<SearchFailure> searchFailures, boolean timedOut) {
scrollSource.close();
if (failure == null) {
listener.onResponse(
buildResponse(timeValueNanos(System.nanoTime() - startTime.get()), indexingFailures, searchFailures, timedOut));
} else {
listener.onFailure(failure);
}
protected void finishHim(Exception failure, List<Failure> indexingFailures,
List<SearchFailure> searchFailures, boolean timedOut) {
scrollSource.close(() -> {
if (failure == null) {
BulkByScrollResponse response = buildResponse(
timeValueNanos(System.nanoTime() - startTime.get()),
indexingFailures, searchFailures, timedOut);
listener.onResponse(response);
} else {
listener.onFailure(failure);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public void onFailure(Exception e) {
}

@Override
protected void cleanup() {
// Nothing to do
protected void cleanup(Runnable onCompletion) {
onCompletion.run();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/**
* A scrollable source of results.
*/
public abstract class ScrollableHitSource implements Closeable {
public abstract class ScrollableHitSource {
private final AtomicReference<String> scrollId = new AtomicReference<>();

protected final Logger logger;
Expand Down Expand Up @@ -82,25 +82,31 @@ public final void startNextScroll(TimeValue extraKeepAlive, Consumer<Response> o
}
protected abstract void doStartNextScroll(String scrollId, TimeValue extraKeepAlive, Consumer<? super Response> onResponse);

@Override
public final void close() {
public final void close(Runnable onCompletion) {
String scrollId = this.scrollId.get();
if (Strings.hasLength(scrollId)) {
clearScroll(scrollId, this::cleanup);
clearScroll(scrollId, () -> cleanup(onCompletion));
} else {
cleanup();
cleanup(onCompletion);
}
}

/**
* Called to clear a scroll id.
*
* @param scrollId the id to clear
* @param onCompletion implementers must call this after completing the clear whether they are successful or not
* @param onCompletion implementers must call this after completing the clear whether they are
* successful or not
*/
protected abstract void clearScroll(String scrollId, Runnable onCompletion);
/**
* Called after the process has been totally finished to clean up any resources the process needed like remote connections.
* Called after the process has been totally finished to clean up any resources the process
* needed like remote connections.
*
* @param onCompletion implementers must call this after completing the cleanup whether they are
* successful or not
*/
protected abstract void cleanup();
protected abstract void cleanup(Runnable onCompletion);

/**
* Set the id of the last scroll. Used for debugging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,18 @@ private void logFailure(Exception e) {
}

@Override
protected void cleanup() {
/* This is called on the RestClient's thread pool and attempting to close the client on its own threadpool causes it to fail to
* close. So we always shutdown the RestClient asynchronously on a thread in Elasticsearch's generic thread pool. */
protected void cleanup(Runnable onCompletion) {
/* This is called on the RestClient's thread pool and attempting to close the client on its
* own threadpool causes it to fail to close. So we always shutdown the RestClient
* asynchronously on a thread in Elasticsearch's generic thread pool. */
threadPool.generic().submit(() -> {
try {
client.close();
logger.debug("Shut down remote connection");
} catch (IOException e) {
logger.error("Failed to shutdown the remote connection", e);
} finally {
onCompletion.run();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class RemoteScrollableHitSourceTests extends ESTestCase {
Expand Down Expand Up @@ -478,6 +480,25 @@ public void testUnexpectedJsonThinksRemoveIsNotES() throws IOException {
e.getCause().getCause().getCause().getMessage());
}

public void testCleanupSuccessful() throws Exception {
AtomicBoolean cleanupCallbackCalled = new AtomicBoolean();
RestClient client = mock(RestClient.class);
TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(client);
hitSource.cleanup(() -> cleanupCallbackCalled.set(true));
verify(client).close();
assertTrue(cleanupCallbackCalled.get());
}

public void testCleanupFailure() throws Exception {
AtomicBoolean cleanupCallbackCalled = new AtomicBoolean();
RestClient client = mock(RestClient.class);
doThrow(new RuntimeException("test")).when(client).close();
TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(client);
hitSource.cleanup(() -> cleanupCallbackCalled.set(true));
verify(client).close();
assertTrue(cleanupCallbackCalled.get());
}

private RemoteScrollableHitSource sourceWithMockedRemoteCall(String... paths) throws Exception {
return sourceWithMockedRemoteCall(true, ContentType.APPLICATION_JSON, paths);
}
Expand Down