Skip to content

Commit

Permalink
Deprecate types in rollover index API (#38389) (#38458)
Browse files Browse the repository at this point in the history
Backport for #38039

Relates to #35190
  • Loading branch information
mayya-sharipova authored Feb 5, 2019
1 parent 1f2cd9f commit aa9816c
Show file tree
Hide file tree
Showing 23 changed files with 784 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
Expand All @@ -65,6 +63,8 @@
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.client.indices.rollover.RolloverResponse;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
Expand Down Expand Up @@ -1317,17 +1317,54 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, RequestOptions
RolloverResponse::fromXContent, emptySet());
}

/**
* Asynchronously rolls over an index using the Rollover Index API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
* Rollover Index API on elastic.co</a>
* @param rolloverRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
RolloverResponse::fromXContent, listener, emptySet());
}

/**
* Rolls over an index using the Rollover Index API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
* Rollover Index API on elastic.co</a>
* @param rolloverRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*
* @deprecated This method uses deprecated request and response objects.
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
*/
@Deprecated
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet());
}

/**
* Rolls over an index using the Rollover Index API.
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
* Rollover Index API on elastic.co</a>
* @deprecated Prefer {@link #rollover(RolloverRequest, RequestOptions)}
*
* @deprecated This method uses deprecated request and response objects.
* The method {@link #rollover(RolloverRequest, RequestOptions)} should be used instead, which accepts a new request object.
*/
@Deprecated
public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... headers) throws IOException {
public org.elasticsearch.action.admin.indices.rollover.RolloverResponse rollover(
org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
RolloverResponse::fromXContent, emptySet(), headers);
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, emptySet(), headers);
}

/**
Expand All @@ -1337,23 +1374,33 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, Header... head
* @param rolloverRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*
* @deprecated This method uses deprecated request and response objects.
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
* accepts a new request object.
*/
public void rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener<RolloverResponse> listener) {
@Deprecated
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
RequestOptions options, ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options,
RolloverResponse::fromXContent, listener, emptySet());
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet());
}

/**
* Asynchronously rolls over an index using the Rollover Index API.
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-rollover-index.html">
* Rollover Index API on elastic.co</a>
* @deprecated Prefer {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)}
*
* @deprecated This method uses deprecated request and response objects.
* The method {@link #rolloverAsync(RolloverRequest, RequestOptions, ActionListener)} should be used instead, which
* accepts a new request object.
*/
@Deprecated
public void rolloverAsync(RolloverRequest rolloverRequest, ActionListener<RolloverResponse> listener, Header... headers) {
public void rolloverAsync(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest,
ActionListener<org.elasticsearch.action.admin.indices.rollover.RolloverResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover,
RolloverResponse::fromXContent, listener, emptySet(), headers);
org.elasticsearch.action.admin.indices.rollover.RolloverResponse::fromXContent, listener, emptySet(), headers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
Expand All @@ -53,6 +52,7 @@
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
import org.elasticsearch.client.indices.rollover.RolloverRequest;
import org.elasticsearch.common.Strings;

import java.io.IOException;
Expand Down Expand Up @@ -348,7 +348,7 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {

static Request rollover(RolloverRequest rolloverRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
.addPathPart(rolloverRequest.getNewIndexName()).build();
.addPathPart(rolloverRequest.getNewIndexName()).build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);

RequestConverters.Params params = new RequestConverters.Params(request);
Expand All @@ -358,11 +358,31 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
if (rolloverRequest.isDryRun()) {
params.putParam("dry_run", Boolean.TRUE.toString());
}
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "false");

request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
return request;
}

@Deprecated
static Request rollover(org.elasticsearch.action.admin.indices.rollover.RolloverRequest rolloverRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover")
.addPathPart(rolloverRequest.getNewIndexName()).build();
Request request = new Request(HttpPost.METHOD_NAME, endpoint);

RequestConverters.Params params = new RequestConverters.Params(request);
params.withTimeout(rolloverRequest.timeout());
params.withMasterTimeout(rolloverRequest.masterNodeTimeout());
params.withWaitForActiveShards(rolloverRequest.getCreateIndexRequest().waitForActiveShards());
if (rolloverRequest.isDryRun()) {
params.putParam("dry_run", Boolean.TRUE.toString());
}
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
request.setEntity(RequestConverters.createEntity(rolloverRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));

return request;
}

static Request getSettings(GetSettingsRequest getSettingsRequest) {
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
String[] names = getSettingsRequest.names() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.names();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ public abstract class TimedRequest implements Validatable {
private TimeValue timeout = DEFAULT_ACK_TIMEOUT;
private TimeValue masterTimeout = DEFAULT_MASTER_NODE_TIMEOUT;

/**
* Sets the timeout to wait for the all the nodes to acknowledge
* @param timeout timeout as a {@link TimeValue}
*/
public void setTimeout(TimeValue timeout) {
this.timeout = timeout;

}

/**
* Sets the timeout to connect to the master node
* @param masterTimeout timeout as a {@link TimeValue}
*/
public void setMasterTimeout(TimeValue masterTimeout) {
this.masterTimeout = masterTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,14 @@ public CreateIndexRequest waitForActiveShards(ActiveShardCount waitForActiveShar
return this;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
innerToXContent(builder, params);
builder.endObject();
return builder;
}

public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(SETTINGS.getPreferredName());
settings.toXContent(builder, params);
builder.endObject();
Expand All @@ -356,8 +360,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
for (Alias alias : aliases) {
alias.toXContent(builder, params);
}
builder.endObject();

builder.endObject();
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices.rollover;

import org.elasticsearch.action.admin.indices.rollover.Condition;
import org.elasticsearch.action.admin.indices.rollover.MaxAgeCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxDocsCondition;
import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition;
import org.elasticsearch.client.TimedRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Request class to swap index under an alias upon satisfying conditions
*/
public class RolloverRequest extends TimedRequest implements ToXContentObject {

private final String alias;
private final String newIndexName;
private boolean dryRun;
private final Map<String, Condition<?>> conditions = new HashMap<>(2);
//the index name "_na_" is never read back, what matters are settings, mappings and aliases
private final CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_");

public RolloverRequest(String alias, String newIndexName) {
if (alias == null) {
throw new IllegalArgumentException("The index alias cannot be null!");
}
this.alias = alias;
this.newIndexName = newIndexName;
}

/**
* Returns the alias of the rollover operation
*/
public String getAlias() {
return alias;
}

/**
* Returns the new index name for the rollover
*/
public String getNewIndexName() {
return newIndexName;
}


/**
* Sets if the rollover should not be executed when conditions are met
*/
public RolloverRequest dryRun(boolean dryRun) {
this.dryRun = dryRun;
return this;
}
/**
* Returns if the rollover should not be executed when conditions are met
*/
public boolean isDryRun() {
return dryRun;
}

/**
* Adds condition to check if the index is at least <code>age</code> old
*/
public RolloverRequest addMaxIndexAgeCondition(TimeValue age) {
MaxAgeCondition maxAgeCondition = new MaxAgeCondition(age);
if (this.conditions.containsKey(maxAgeCondition.name())) {
throw new IllegalArgumentException(maxAgeCondition.name() + " condition is already set");
}
this.conditions.put(maxAgeCondition.name(), maxAgeCondition);
return this;
}

/**
* Adds condition to check if the index has at least <code>numDocs</code>
*/
public RolloverRequest addMaxIndexDocsCondition(long numDocs) {
MaxDocsCondition maxDocsCondition = new MaxDocsCondition(numDocs);
if (this.conditions.containsKey(maxDocsCondition.name())) {
throw new IllegalArgumentException(maxDocsCondition.name() + " condition is already set");
}
this.conditions.put(maxDocsCondition.name(), maxDocsCondition);
return this;
}
/**
* Adds a size-based condition to check if the index size is at least <code>size</code>.
*/
public RolloverRequest addMaxIndexSizeCondition(ByteSizeValue size) {
MaxSizeCondition maxSizeCondition = new MaxSizeCondition(size);
if (this.conditions.containsKey(maxSizeCondition.name())) {
throw new IllegalArgumentException(maxSizeCondition + " condition is already set");
}
this.conditions.put(maxSizeCondition.name(), maxSizeCondition);
return this;
}
/**
* Returns all set conditions
*/
public Map<String, Condition<?>> getConditions() {
return conditions;
}

/**
* Returns the inner {@link CreateIndexRequest}. Allows to configure mappings, settings and aliases for the new index.
*/
public CreateIndexRequest getCreateIndexRequest() {
return createIndexRequest;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
createIndexRequest.innerToXContent(builder, params);

builder.startObject("conditions");
for (Condition<?> condition : conditions.values()) {
condition.toXContent(builder, params);
}
builder.endObject();

builder.endObject();
return builder;
}

}
Loading

0 comments on commit aa9816c

Please sign in to comment.