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

Retry ILM steps that fail due to SnapshotInProgressException #37624

Merged
merged 22 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7aeec21
add test for running certain ILM actions during snapshotting
talevy Jan 16, 2019
a97d0eb
swap the tests to awaitsfix and test that things succeed
talevy Jan 17, 2019
e8c43de
fix getSnapshotState
talevy Jan 17, 2019
273b932
fix checkstyle
talevy Jan 17, 2019
d57f589
Merge remote-tracking branch 'upstream/master' into ilm-snapshot-test
talevy Jan 17, 2019
d15507a
WIP
dakrone Jan 16, 2019
545a40d
Add RetryDuringSnapshotStep
dakrone Jan 17, 2019
af6ad54
Move DeleteStep to use RetryDuringSnapshotStep
dakrone Jan 17, 2019
8cf8852
Move to real SnapshotInProgressException
dakrone Jan 17, 2019
b5ff014
Add license header
dakrone Jan 17, 2019
ce1f998
Call original listener `onFailure` if it was not a snapshot exception
dakrone Jan 17, 2019
cc2b329
Checkstyle line length fixes
dakrone Jan 17, 2019
b36c48a
Use RetryDuringSnapshotStep for FreezeStep as well
dakrone Jan 17, 2019
cd2bad7
Merge remote-tracking branch 'talevy/ilm-snapshot-test' into ilm-retr…
dakrone Jan 17, 2019
2d5dd8d
Unawaitsfix the tests, fix RetryDuringSnapshotStep
dakrone Jan 18, 2019
2b1746c
Merge remote-tracking branch 'origin/master' into ilm-retry-after-sna…
dakrone Jan 18, 2019
f1fb55f
Fix for unfollow steps after master merge
dakrone Jan 18, 2019
9818012
Move CloseFollowerIndexStep to extend RetryDuringSnapshotStep
dakrone Jan 18, 2019
76099b9
Add a test for unfollow while a snapshot is ongoing
dakrone Jan 18, 2019
34f9444
Add some debug logging for the snapshot retry
dakrone Jan 18, 2019
f95b290
Rename RetryDuringSnapshotStep -> AsyncRetryDuringSnapshotActionStep
dakrone Jan 18, 2019
ffdc5fd
Be paranoid about exceptions being thrown and swallowed on accident
dakrone Jan 18, 2019
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 @@ -7,6 +7,7 @@

import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;

import java.util.Map;
Expand All @@ -20,7 +21,8 @@ abstract class AbstractUnfollowIndexStep extends AsyncActionStep {
}

@Override
public final void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener) {
public final void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
ClusterStateObserver observer, Listener listener) {
String followerIndex = indexMetaData.getIndex().getName();
Map<String, String> customIndexMetadata = indexMetaData.getCustomData(CCR_METADATA_KEY);
if (customIndexMetadata == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;

/**
Expand All @@ -29,7 +30,8 @@ public boolean indexSurvives() {
return true;
}

public abstract void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener);
public abstract void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
ClusterStateObserver observer, Listener listener);

public interface Listener {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

package org.elasticsearch.xpack.core.indexlifecycle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.repositories.IndexId;
import org.elasticsearch.snapshots.SnapshotInProgressException;

import java.util.function.Consumer;

/**
* This is an abstract AsyncActionStep that wraps the performed action listener, checking to see
* if the action fails due to a snapshot being in progress. If a snapshot is in progress, it
* registers an observer and waits to try again when a snapshot is no longer running.
*/
public abstract class AsyncRetryDuringSnapshotActionStep extends AsyncActionStep {
private final Logger logger = LogManager.getLogger(AsyncRetryDuringSnapshotActionStep.class);

public AsyncRetryDuringSnapshotActionStep(StepKey key, StepKey nextStepKey, Client client) {
super(key, nextStepKey, client);
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
ClusterStateObserver observer, Listener listener) {
// Wrap the original listener to handle exceptions caused by ongoing snapshots
SnapshotExceptionListener snapshotExceptionListener = new SnapshotExceptionListener(indexMetaData.getIndex(), listener, observer);
performDuringNoSnapshot(indexMetaData, currentClusterState, snapshotExceptionListener);
}

/**
* Method to be performed during which no snapshots for the index are already underway.
*/
abstract void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener);

/**
* SnapshotExceptionListener is an injected listener wrapper that checks to see if a particular
* action failed due to a {@code SnapshotInProgressException}. If it did, then it registers a
* ClusterStateObserver listener waiting for the next time the snapshot is not running,
* re-running the step's {@link #performAction(IndexMetaData, ClusterState, ClusterStateObserver, Listener)}
* method when the snapshot is no longer running.
*/
class SnapshotExceptionListener implements AsyncActionStep.Listener {
private final Index index;
private final Listener originalListener;
private final ClusterStateObserver observer;

SnapshotExceptionListener(Index index, Listener originalListener, ClusterStateObserver observer) {
this.index = index;
this.originalListener = originalListener;
this.observer = observer;
}

@Override
public void onResponse(boolean complete) {
originalListener.onResponse(complete);
}

@Override
public void onFailure(Exception e) {
if (e instanceof SnapshotInProgressException) {
try {
logger.debug("[{}] attempted to run ILM step but a snapshot is in progress, step will retry at a later time",
index.getName());
observer.waitForNextChange(
new NoSnapshotRunningListener(observer, index.getName(), state -> {
IndexMetaData idxMeta = state.metaData().index(index);
if (idxMeta == null) {
// The index has since been deleted, mission accomplished!
originalListener.onResponse(true);
}
// Re-invoke the performAction method with the new state
performAction(idxMeta, state, observer, originalListener);
}, originalListener::onFailure),
// TODO: what is a good timeout value for no new state received during this time?
TimeValue.timeValueHours(12));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think waiting 12 hours for a snapshot to finish is reasonable. If there is no progress on this action in that time interval, a user may want to know. so 👍

} catch (Exception secondError) {
// There was a second error trying to set up an observer,
// fail the original listener
secondError.addSuppressed(e);
originalListener.onFailure(secondError);
}
} else {
originalListener.onFailure(e);
}
}
}

/**
* A {@link ClusterStateObserver.Listener} that invokes the given function with the new state,
* once no snapshots are running. If a snapshot is still running it registers a new listener
* and tries again. Passes any exceptions to the original exception listener if they occur.
*/
class NoSnapshotRunningListener implements ClusterStateObserver.Listener {

private final Consumer<ClusterState> reRun;
private final Consumer<Exception> exceptionConsumer;
private final ClusterStateObserver observer;
private final String indexName;

NoSnapshotRunningListener(ClusterStateObserver observer, String indexName,
Consumer<ClusterState> reRun,
Consumer<Exception> exceptionConsumer) {
this.observer = observer;
this.reRun = reRun;
this.exceptionConsumer = exceptionConsumer;
this.indexName = indexName;
}

@Override
public void onNewClusterState(ClusterState state) {
try {
if (snapshotInProgress(state)) {
observer.waitForNextChange(this);
} else {
logger.debug("[{}] retrying ILM step after snapshot has completed", indexName);
reRun.accept(state);
}
} catch (Exception e) {
exceptionConsumer.accept(e);
}
}

private boolean snapshotInProgress(ClusterState state) {
SnapshotsInProgress snapshotsInProgress = state.custom(SnapshotsInProgress.TYPE);
if (snapshotsInProgress == null || snapshotsInProgress.entries().isEmpty()) {
// No snapshots are running, new state is acceptable to proceed
return false;
}

for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) {
if (snapshot.indices().stream()
.map(IndexId::getName)
.anyMatch(name -> name.equals(indexName))) {
// There is a snapshot running with this index name
return true;
}
}
// There are snapshots, but none for this index, so it's okay to proceed with this state
return false;
}

@Override
public void onClusterServiceClose() {
// This means the cluster is being shut down, so nothing to do here
}

@Override
public void onTimeout(TimeValue timeout) {
exceptionConsumer.accept(new IllegalStateException("step timed out while waiting for snapshots to complete"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;

final class CloseFollowerIndexStep extends AbstractUnfollowIndexStep {
import java.util.Map;

import static org.elasticsearch.xpack.core.indexlifecycle.UnfollowAction.CCR_METADATA_KEY;

final class CloseFollowerIndexStep extends AsyncRetryDuringSnapshotActionStep {

static final String NAME = "close-follower-index";

Expand All @@ -18,7 +24,14 @@ final class CloseFollowerIndexStep extends AbstractUnfollowIndexStep {
}

@Override
void innerPerformAction(String followerIndex, Listener listener) {
void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener) {
String followerIndex = indexMetaData.getIndex().getName();
Map<String, String> customIndexMetadata = indexMetaData.getCustomData(CCR_METADATA_KEY);
if (customIndexMetadata == null) {
listener.onResponse(true);
return;
}

CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex);
getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap(
r -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
/**
* Deletes a single index.
*/
public class DeleteStep extends AsyncActionStep {
public class DeleteStep extends AsyncRetryDuringSnapshotActionStep {
public static final String NAME = "delete";

public DeleteStep(StepKey key, StepKey nextStepKey, Client client) {
super(key, nextStepKey, client);
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
getClient().admin().indices()
.delete(new DeleteIndexRequest(indexMetaData.getIndex().getName()),
ActionListener.wrap(response -> listener.onResponse(true) , listener::onFailure));
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;

import java.util.Objects;
Expand All @@ -30,7 +31,7 @@ public int getMaxNumSegments() {
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, ClusterStateObserver observer, Listener listener) {
ForceMergeRequest request = new ForceMergeRequest(indexMetaData.getIndex().getName());
request.maxNumSegments(maxNumSegments);
getClient().admin().indices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
/**
* Freezes an index.
*/
public class FreezeStep extends AsyncActionStep {
public class FreezeStep extends AsyncRetryDuringSnapshotActionStep {
public static final String NAME = "freeze";

public FreezeStep(StepKey key, StepKey nextStepKey, Client client) {
super(key, nextStepKey, client);
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
getClient().admin().indices().execute(TransportFreezeIndexAction.FreezeIndexAction.INSTANCE,
new TransportFreezeIndexAction.FreezeRequest(indexMetaData.getIndex().getName()),
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;

final class OpenFollowerIndexStep extends AsyncActionStep {
Expand All @@ -20,7 +21,8 @@ final class OpenFollowerIndexStep extends AsyncActionStep {
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
ClusterStateObserver observer, Listener listener) {
OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName());
getClient().admin().indices().open(request, ActionListener.wrap(
r -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;

Expand All @@ -30,7 +31,8 @@ public RolloverStep(StepKey key, StepKey nextStepKey, Client client) {
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
ClusterStateObserver observer, Listener listener) {
boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(indexMetaData.getSettings());
if (indexingComplete) {
logger.trace(indexMetaData.getIndex() + " has lifecycle complete set, skipping " + RolloverStep.NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.RoutingNode;
Expand Down Expand Up @@ -42,7 +43,7 @@ public SetSingleNodeAllocateStep(StepKey key, StepKey nextStepKey, Client client
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState clusterState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState clusterState, ClusterStateObserver observer, Listener listener) {
RoutingAllocation allocation = new RoutingAllocation(ALLOCATION_DECIDERS, clusterState.getRoutingNodes(), clusterState, null,
System.nanoTime());
List<String> validNodeIds = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Following shrinking an index and deleting the original index, this step creates an alias with the same name as the original index which
* points to the new shrunken index to allow clients to continue to use the original index name without being aware that it has shrunk.
*/
public class ShrinkSetAliasStep extends AsyncActionStep {
public class ShrinkSetAliasStep extends AsyncRetryDuringSnapshotActionStep {
public static final String NAME = "aliases";
private String shrunkIndexPrefix;

Expand All @@ -32,7 +32,7 @@ String getShrunkIndexPrefix() {
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
// get source index
String index = indexMetaData.getIndex().getName();
// get target shrink index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;

Expand Down Expand Up @@ -38,7 +39,7 @@ String getShrunkIndexPrefix() {
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, ClusterStateObserver observer, Listener listener) {
LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData);
if (lifecycleState.getLifecycleDate() == null) {
throw new IllegalStateException("source index [" + indexMetaData.getIndex().getName() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateObserver;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;

Expand All @@ -28,7 +29,7 @@ public UpdateSettingsStep(StepKey key, StepKey nextStepKey, Client client, Setti
}

@Override
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) {
public void performAction(IndexMetaData indexMetaData, ClusterState currentState, ClusterStateObserver observer, Listener listener) {
UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetaData.getIndex().getName()).settings(settings);
getClient().admin().indices().updateSettings(updateSettingsRequest,
ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure));
Expand Down
Loading