conditionally update CS only if StepInfo changes (#33004)

If we are waiting on a condition to be met, and the reason
it is not completed is unchanged, we find ourselves updating
cluster state over and over again and kicking of the ILM listeners
to re-check. This is overkill and can generate way too many
cluster state updates
This commit is contained in:
Tal Levy 2018-08-21 12:29:41 -07:00 committed by GitHub
parent 6780ab9d5c
commit 96869b253c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -278,13 +278,30 @@ public class IndexLifecycleRunner {
return newClusterStateBuilder;
}
/**
* Conditionally updates cluster state with new step info. The new cluster state is only
* built if the step info has changed, otherwise the same old <code>clusterState</code> is
* returned
*
* @param index the index to modify
* @param clusterState the cluster state to modify
* @param stepInfo the new step info to update
* @return Updated cluster state with <code>stepInfo</code> if changed, otherwise the same cluster state
* if no changes to step info exist
* @throws IOException if parsing step info fails
*/
static ClusterState addStepInfoToClusterState(Index index, ClusterState clusterState, ToXContentObject stepInfo) throws IOException {
IndexMetaData idxMeta = clusterState.getMetaData().index(index);
XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder();
stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS);
String stepInfoString = BytesReference.bytes(infoXContentBuilder).utf8ToString();
final String stepInfoString;
try (XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder()) {
stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS);
stepInfoString = BytesReference.bytes(infoXContentBuilder).utf8ToString();
}
if (stepInfoString.equals(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(idxMeta.getSettings()))) {
return clusterState;
}
Settings.Builder indexSettings = Settings.builder().put(idxMeta.getSettings())
.put(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey(), stepInfoString);
.put(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey(), stepInfoString);
ClusterState.Builder newClusterStateBuilder = newClusterStateWithIndexSettings(index, clusterState, indexSettings);
return newClusterStateBuilder.build();
}

View File

@ -820,6 +820,8 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
Index index = clusterState.metaData().index(indexName).getIndex();
ClusterState newClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, clusterState, stepInfo);
assertClusterStateStepInfo(clusterState, index, currentStep, newClusterState, stepInfo);
ClusterState runAgainClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, newClusterState, stepInfo);
assertSame(newClusterState, runAgainClusterState);
}
@SuppressWarnings("unchecked")