diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/LazyRolloverAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/LazyRolloverAction.java index 1fecfc3b70375..6560fd4f0a29f 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/LazyRolloverAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/LazyRolloverAction.java @@ -268,6 +268,11 @@ public ClusterState executeTask( final var rolloverIndexName = rolloverResult.rolloverIndexName(); final var sourceIndexName = rolloverResult.sourceIndexName(); + logger.info( + "rolling over data stream [{}] to index [{}] because it was marked for lazy rollover", + dataStream.getName(), + rolloverIndexName + ); final var waitForActiveShardsTimeout = rolloverRequest.masterNodeTimeout().millis() < 0 ? null diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java index eeac384188b12..b3a8d90c668b9 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java @@ -221,45 +221,17 @@ protected void masterOperation( final String trialRolloverIndexName = trialRolloverNames.rolloverName(); MetadataCreateIndexService.validateIndexName(trialRolloverIndexName, projectMetadata, projectState.routingTable()); - boolean isDataStream = projectMetadata.dataStreams().containsKey(resolvedRolloverTarget.resource()); if (rolloverRequest.isLazy()) { - if (isDataStream == false || rolloverRequest.getConditions().hasConditions()) { - String message; - if (isDataStream) { - message = "Lazy rollover can be used only without any conditions." - + " Please remove the conditions from the request body or the query parameter 'lazy'."; - } else if (rolloverRequest.getConditions().hasConditions() == false) { - message = "Lazy rollover can be applied only on a data stream." + " Please remove the query parameter 'lazy'."; - } else { - message = "Lazy rollover can be applied only on a data stream with no conditions." - + " Please remove the query parameter 'lazy'."; - } - listener.onFailure(new IllegalArgumentException(message)); - return; - } - if (rolloverRequest.isDryRun() == false) { - metadataDataStreamsService.setRolloverOnWrite( - projectState.projectId(), - resolvedRolloverTarget.resource(), - true, - targetFailureStore, - rolloverRequest.ackTimeout(), - rolloverRequest.masterNodeTimeout(), - listener.map( - response -> new RolloverResponse( - trialSourceIndexName, - trialRolloverIndexName, - Map.of(), - false, - false, - response.isAcknowledged(), - false, - response.isAcknowledged() - ) - ) - ); - return; - } + markForLazyRollover( + rolloverRequest, + listener, + projectMetadata, + resolvedRolloverTarget, + targetFailureStore, + trialSourceIndexName, + trialRolloverIndexName + ); + return; } final IndexAbstraction rolloverTargetAbstraction = projectMetadata.getIndicesLookup().get(resolvedRolloverTarget.resource()); @@ -353,7 +325,7 @@ protected void masterOperation( false, false, false, - rolloverRequest.isLazy() + false ); // If this is a dry run, return with the results without invoking a cluster state update @@ -382,6 +354,58 @@ protected void masterOperation( ); } + private void markForLazyRollover( + RolloverRequest rolloverRequest, + ActionListener listener, + ProjectMetadata projectMetadata, + ResolvedExpression resolvedRolloverTarget, + boolean targetFailureStore, + String trialSourceIndexName, + String trialRolloverIndexName + ) { + boolean isDataStream = projectMetadata.dataStreams().containsKey(resolvedRolloverTarget.resource()); + if (isDataStream == false || rolloverRequest.getConditions().hasConditions()) { + String message; + if (isDataStream) { + message = "Lazy rollover can be used only without any conditions." + + " Please remove the conditions from the request body or the query parameter 'lazy'."; + } else if (rolloverRequest.getConditions().hasConditions() == false) { + message = "Lazy rollover can be applied only on a data stream. Please remove the query parameter 'lazy'."; + } else { + message = "Lazy rollover can be applied only on a data stream with no conditions." + + " Please remove the query parameter 'lazy'."; + } + listener.onFailure(new IllegalArgumentException(message)); + return; + } + if (rolloverRequest.isDryRun()) { + listener.onResponse( + new RolloverResponse(trialSourceIndexName, trialRolloverIndexName, Map.of(), true, false, false, false, true) + ); + return; + } + metadataDataStreamsService.setRolloverOnWrite( + projectMetadata.id(), + resolvedRolloverTarget.resource(), + true, + targetFailureStore, + rolloverRequest.ackTimeout(), + rolloverRequest.masterNodeTimeout(), + listener.map( + response -> new RolloverResponse( + trialSourceIndexName, + trialRolloverIndexName, + Map.of(), + false, + false, + response.isAcknowledged(), + false, + true + ) + ) + ); + } + private void initializeFailureStore( ProjectId projectId, RolloverRequest rolloverRequest, diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java index 7048208efaa3a..2177733ad3f86 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java @@ -470,19 +470,39 @@ public void testLazyRollover() throws Exception { mockMetadataDataStreamService, dataStreamAutoShardingService ); - final PlainActionFuture future = new PlainActionFuture<>(); - RolloverRequest rolloverRequest = new RolloverRequest("logs-ds", null); - rolloverRequest.lazy(true); - transportRolloverAction.masterOperation(mock(CancellableTask.class), rolloverRequest, stateBefore, future); - RolloverResponse rolloverResponse = future.actionGet(); - assertThat(rolloverResponse.getOldIndex(), equalTo(".ds-logs-ds-000001")); - assertThat(rolloverResponse.getNewIndex(), Matchers.startsWith(".ds-logs-ds-")); - assertThat(rolloverResponse.getNewIndex(), Matchers.endsWith("-000002")); - assertThat(rolloverResponse.isLazy(), equalTo(true)); - assertThat(rolloverResponse.isDryRun(), equalTo(false)); - assertThat(rolloverResponse.isRolledOver(), equalTo(false)); - assertThat(rolloverResponse.getConditionStatus().size(), equalTo(0)); - assertThat(rolloverResponse.isAcknowledged(), is(true)); + { + // Regular lazy rollover + final PlainActionFuture future = new PlainActionFuture<>(); + RolloverRequest rolloverRequest = new RolloverRequest("logs-ds", null); + rolloverRequest.lazy(true); + transportRolloverAction.masterOperation(mock(CancellableTask.class), rolloverRequest, stateBefore, future); + RolloverResponse rolloverResponse = future.actionGet(); + assertThat(rolloverResponse.getOldIndex(), equalTo(".ds-logs-ds-000001")); + assertThat(rolloverResponse.getNewIndex(), Matchers.startsWith(".ds-logs-ds-")); + assertThat(rolloverResponse.getNewIndex(), Matchers.endsWith("-000002")); + assertThat(rolloverResponse.isLazy(), equalTo(true)); + assertThat(rolloverResponse.isDryRun(), equalTo(false)); + assertThat(rolloverResponse.isRolledOver(), equalTo(false)); + assertThat(rolloverResponse.getConditionStatus().size(), equalTo(0)); + assertThat(rolloverResponse.isAcknowledged(), is(true)); + } + { + // Dry-run lazy rollover + final PlainActionFuture future = new PlainActionFuture<>(); + RolloverRequest rolloverRequest = new RolloverRequest("logs-ds", null); + rolloverRequest.lazy(true); + rolloverRequest.dryRun(true); + transportRolloverAction.masterOperation(mock(CancellableTask.class), rolloverRequest, stateBefore, future); + RolloverResponse rolloverResponse = future.actionGet(); + assertThat(rolloverResponse.getOldIndex(), equalTo(".ds-logs-ds-000001")); + assertThat(rolloverResponse.getNewIndex(), Matchers.startsWith(".ds-logs-ds-")); + assertThat(rolloverResponse.getNewIndex(), Matchers.endsWith("-000002")); + assertThat(rolloverResponse.isLazy(), equalTo(true)); + assertThat(rolloverResponse.isDryRun(), equalTo(true)); + assertThat(rolloverResponse.isRolledOver(), equalTo(false)); + assertThat(rolloverResponse.getConditionStatus().size(), equalTo(0)); + assertThat(rolloverResponse.isAcknowledged(), is(false)); + } } public void testLazyRolloverFails() throws Exception { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java index 1d530017124e5..fb06cfb908382 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java @@ -34,6 +34,7 @@ import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.Nullable; @@ -842,9 +843,8 @@ public void onFailure(Exception e) { void onRolloversBulkResponse(Collection rolloverResponses) { for (RolloverResponse rolloverResponse : rolloverResponses) { - if (rolloverResponse.isRolledOver() == false) { - logger.warn("rollover of the [{}] index [{}] failed", getOrigin(), rolloverResponse.getOldIndex()); - } + assert rolloverResponse.isLazy() && rolloverResponse.isRolledOver() == false + : Strings.format("Expected rollover of the [%s] index [%s] to be lazy", getOrigin(), rolloverResponse.getOldIndex()); } }