enhance rollover request
This commit is contained in:
parent
d96fe20e3a
commit
1e329099f9
|
@ -44,10 +44,12 @@ public abstract class Condition<T> implements NamedWriteable {
|
||||||
public final static String NAME = "max_age";
|
public final static String NAME = "max_age";
|
||||||
|
|
||||||
public MaxAge(TimeValue value) {
|
public MaxAge(TimeValue value) {
|
||||||
|
super(NAME);
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MaxAge(StreamInput in) throws IOException {
|
public MaxAge(StreamInput in) throws IOException {
|
||||||
|
super(NAME);
|
||||||
this.value = TimeValue.timeValueMillis(in.readLong());
|
this.value = TimeValue.timeValueMillis(in.readLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,10 +73,12 @@ public abstract class Condition<T> implements NamedWriteable {
|
||||||
public final static String NAME = "max_docs";
|
public final static String NAME = "max_docs";
|
||||||
|
|
||||||
public MaxDocs(Long value) {
|
public MaxDocs(Long value) {
|
||||||
|
super(NAME);
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MaxDocs(StreamInput in) throws IOException {
|
public MaxDocs(StreamInput in) throws IOException {
|
||||||
|
super(NAME);
|
||||||
this.value = in.readLong();
|
this.value = in.readLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +99,16 @@ public abstract class Condition<T> implements NamedWriteable {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected T value;
|
protected T value;
|
||||||
|
protected final String name;
|
||||||
|
|
||||||
|
protected Condition(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public abstract boolean matches(T value);
|
public abstract boolean matches(T value);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String toString() {
|
||||||
|
return "[" + name + ": " + value + "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
|
||||||
public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implements IndicesRequest {
|
public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implements IndicesRequest {
|
||||||
|
|
||||||
private String sourceAlias;
|
private String sourceAlias;
|
||||||
|
private boolean simulate;
|
||||||
private Set<Condition> conditions = new HashSet<>(2);
|
private Set<Condition> conditions = new HashSet<>(2);
|
||||||
|
|
||||||
public static ObjectParser<Set<Condition>, ParseFieldMatcherSupplier> TLP_PARSER =
|
public static ObjectParser<Set<Condition>, ParseFieldMatcherSupplier> TLP_PARSER =
|
||||||
|
@ -77,6 +78,7 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
super.readFrom(in);
|
super.readFrom(in);
|
||||||
sourceAlias = in.readString();
|
sourceAlias = in.readString();
|
||||||
|
simulate = in.readBoolean();
|
||||||
int size = in.readVInt();
|
int size = in.readVInt();
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
this.conditions.add(in.readNamedWriteable(Condition.class));
|
this.conditions.add(in.readNamedWriteable(Condition.class));
|
||||||
|
@ -87,6 +89,7 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
||||||
public void writeTo(StreamOutput out) throws IOException {
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
super.writeTo(out);
|
super.writeTo(out);
|
||||||
out.writeString(sourceAlias);
|
out.writeString(sourceAlias);
|
||||||
|
out.writeBoolean(simulate);
|
||||||
out.writeVInt(conditions.size());
|
out.writeVInt(conditions.size());
|
||||||
for (Condition condition : conditions) {
|
for (Condition condition : conditions) {
|
||||||
out.writeNamedWriteable(condition);
|
out.writeNamedWriteable(condition);
|
||||||
|
@ -107,6 +110,10 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
||||||
this.sourceAlias = sourceAlias;
|
this.sourceAlias = sourceAlias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void simulate(boolean simulate) {
|
||||||
|
this.simulate = simulate;
|
||||||
|
}
|
||||||
|
|
||||||
public void addMaxIndexAgeCondition(TimeValue age) {
|
public void addMaxIndexAgeCondition(TimeValue age) {
|
||||||
this.conditions.add(new Condition.MaxAge(age));
|
this.conditions.add(new Condition.MaxAge(age));
|
||||||
}
|
}
|
||||||
|
@ -115,6 +122,10 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
||||||
this.conditions.add(new Condition.MaxDocs(docs));
|
this.conditions.add(new Condition.MaxDocs(docs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSimulate() {
|
||||||
|
return simulate;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<Condition> getConditions() {
|
public Set<Condition> getConditions() {
|
||||||
return conditions;
|
return conditions;
|
||||||
}
|
}
|
||||||
|
@ -135,4 +146,5 @@ public class RolloverRequest extends AcknowledgedRequest<RolloverRequest> implem
|
||||||
throw new ElasticsearchParseException("failed to parse content type for rollover index source");
|
throw new ElasticsearchParseException("failed to parse content type for rollover index source");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,4 +46,9 @@ public class RolloverRequestBuilder extends MasterNodeOperationRequestBuilder<Ro
|
||||||
this.request.addMaxIndexDocsCondition(docs);
|
this.request.addMaxIndexDocsCondition(docs);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RolloverRequestBuilder simulate(boolean simulate) {
|
||||||
|
this.request.simulate(simulate);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,20 +22,36 @@ package org.elasticsearch.action.admin.indices.rollover;
|
||||||
import org.elasticsearch.action.ActionResponse;
|
import org.elasticsearch.action.ActionResponse;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public final class RolloverResponse extends ActionResponse {
|
public final class RolloverResponse extends ActionResponse implements ToXContent {
|
||||||
|
|
||||||
private String oldIndex;
|
private String oldIndex;
|
||||||
private String newIndex;
|
private String newIndex;
|
||||||
|
private Set<Map.Entry<String, Boolean>> conditionStatus;
|
||||||
|
private boolean simulate;
|
||||||
|
private boolean rolledOver;
|
||||||
|
private boolean rolloverIndexCreated;
|
||||||
|
|
||||||
RolloverResponse() {
|
RolloverResponse() {
|
||||||
}
|
}
|
||||||
|
|
||||||
RolloverResponse(String oldIndex, String newIndex) {
|
RolloverResponse(String oldIndex, String newIndex, Set<Map.Entry<String, Boolean>> conditionStatus,
|
||||||
|
boolean simulate, boolean rolledOver, boolean rolloverIndexCreated) {
|
||||||
this.oldIndex = oldIndex;
|
this.oldIndex = oldIndex;
|
||||||
this.newIndex = newIndex;
|
this.newIndex = newIndex;
|
||||||
|
this.simulate = simulate;
|
||||||
|
this.rolledOver = rolledOver;
|
||||||
|
this.rolloverIndexCreated = rolloverIndexCreated;
|
||||||
|
this.conditionStatus = conditionStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOldIndex() {
|
public String getOldIndex() {
|
||||||
|
@ -46,11 +62,38 @@ public final class RolloverResponse extends ActionResponse {
|
||||||
return newIndex;
|
return newIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<Map.Entry<String, Boolean>> getConditionStatus() {
|
||||||
|
return conditionStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSimulate() {
|
||||||
|
return simulate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRolledOver() {
|
||||||
|
return rolledOver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRolloverIndexCreated() {
|
||||||
|
return rolloverIndexCreated;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
super.readFrom(in);
|
super.readFrom(in);
|
||||||
oldIndex = in.readString();
|
oldIndex = in.readString();
|
||||||
newIndex = in.readString();
|
newIndex = in.readString();
|
||||||
|
int conditionSize = in.readVInt();
|
||||||
|
Set<Map.Entry<String, Boolean>> conditions = new HashSet<>(conditionSize);
|
||||||
|
for (int i = 0; i < conditionSize; i++) {
|
||||||
|
String condition = in.readString();
|
||||||
|
boolean satisfied = in.readBoolean();
|
||||||
|
conditions.add(new AbstractMap.SimpleEntry<>(condition, satisfied));
|
||||||
|
}
|
||||||
|
conditionStatus = conditions;
|
||||||
|
simulate = in.readBoolean();
|
||||||
|
rolledOver = in.readBoolean();
|
||||||
|
rolloverIndexCreated = in.readBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -58,5 +101,37 @@ public final class RolloverResponse extends ActionResponse {
|
||||||
super.writeTo(out);
|
super.writeTo(out);
|
||||||
out.writeString(oldIndex);
|
out.writeString(oldIndex);
|
||||||
out.writeString(newIndex);
|
out.writeString(newIndex);
|
||||||
|
out.writeVInt(conditionStatus.size());
|
||||||
|
for (Map.Entry<String, Boolean> entry : conditionStatus) {
|
||||||
|
out.writeString(entry.getKey());
|
||||||
|
out.writeBoolean(entry.getValue());
|
||||||
|
}
|
||||||
|
out.writeBoolean(simulate);
|
||||||
|
out.writeBoolean(rolledOver);
|
||||||
|
out.writeBoolean(rolloverIndexCreated);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
|
builder.field(Fields.OLD_INDEX, oldIndex);
|
||||||
|
builder.field(Fields.NEW_INDEX, newIndex);
|
||||||
|
builder.field(Fields.ROLLED_OVER, rolledOver);
|
||||||
|
builder.field(Fields.SIMULATED, simulate);
|
||||||
|
builder.field(Fields.ROLLOVER_INDEX_CREATED, rolloverIndexCreated);
|
||||||
|
builder.startObject(Fields.CONDITIONS);
|
||||||
|
for (Map.Entry<String, Boolean> entry : conditionStatus) {
|
||||||
|
builder.field(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
builder.endObject();
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class Fields {
|
||||||
|
static final String NEW_INDEX = "new_index";
|
||||||
|
static final String OLD_INDEX = "old_index";
|
||||||
|
static final String SIMULATED = "simulated";
|
||||||
|
static final String ROLLED_OVER = "rolled_over";
|
||||||
|
static final String ROLLOVER_INDEX_CREATED = "rollover_index_created";
|
||||||
|
static final String CONDITIONS = "conditions";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,11 @@ import org.elasticsearch.indices.IndexAlreadyExistsException;
|
||||||
import org.elasticsearch.threadpool.ThreadPool;
|
import org.elasticsearch.threadpool.ThreadPool;
|
||||||
import org.elasticsearch.transport.TransportService;
|
import org.elasticsearch.transport.TransportService;
|
||||||
|
|
||||||
|
import java.util.AbstractMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main class to swap the index pointed to by an alias, given some predicates
|
* Main class to swap the index pointed to by an alias, given some predicates
|
||||||
|
@ -105,9 +109,23 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
||||||
DocsStats docsStats = indicesStatsResponse.getTotal().getDocs();
|
DocsStats docsStats = indicesStatsResponse.getTotal().getDocs();
|
||||||
long docCount = docsStats == null ? 0 : docsStats.getCount();
|
long docCount = docsStats == null ? 0 : docsStats.getCount();
|
||||||
long indexAge = System.currentTimeMillis() - sourceIndex.getCreationDate();
|
long indexAge = System.currentTimeMillis() - sourceIndex.getCreationDate();
|
||||||
if (satisfiesConditions(rolloverRequest.getConditions(), docCount, indexAge)) {
|
final Set<Map.Entry<Condition, Boolean>> evaluatedConditions =
|
||||||
final String rolloverIndexName = generateRolloverIndexName(sourceIndexName);
|
evaluateConditions(rolloverRequest.getConditions(), docCount, indexAge);
|
||||||
boolean createRolloverIndex = metaData.index(rolloverIndexName) == null;
|
final Set<Map.Entry<String, Boolean>> conditionStatus = evaluatedConditions.stream()
|
||||||
|
.map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey().toString(), entry.getValue()))
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
final String rolloverIndexName = generateRolloverIndexName(sourceIndexName);
|
||||||
|
final boolean createRolloverIndex = metaData.index(rolloverIndexName) == null;
|
||||||
|
if (rolloverRequest.isSimulate()) {
|
||||||
|
listener.onResponse(
|
||||||
|
new RolloverResponse(sourceIndexName, rolloverIndexName, conditionStatus, true, false,
|
||||||
|
false));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (conditionStatus.stream().allMatch(Map.Entry::getValue)) {
|
||||||
|
final RolloverResponse rolloverResponse =
|
||||||
|
new RolloverResponse(sourceIndexName, rolloverIndexName, conditionStatus, false, true,
|
||||||
|
createRolloverIndex);
|
||||||
if (createRolloverIndex) {
|
if (createRolloverIndex) {
|
||||||
CreateIndexClusterStateUpdateRequest updateRequest =
|
CreateIndexClusterStateUpdateRequest updateRequest =
|
||||||
prepareCreateIndexRequest(rolloverIndexName, rolloverRequest);
|
prepareCreateIndexRequest(rolloverIndexName, rolloverRequest);
|
||||||
|
@ -116,7 +134,7 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
||||||
public void onResponse(ClusterStateUpdateResponse response) {
|
public void onResponse(ClusterStateUpdateResponse response) {
|
||||||
indexAliasesService.indicesAliases(
|
indexAliasesService.indicesAliases(
|
||||||
prepareIndicesAliasesRequest(sourceIndexName, rolloverIndexName, rolloverRequest),
|
prepareIndicesAliasesRequest(sourceIndexName, rolloverIndexName, rolloverRequest),
|
||||||
new IndicesAliasesListener(sourceIndexName, rolloverIndexName, listener));
|
new IndicesAliasesListener(rolloverResponse, listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -132,11 +150,14 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
||||||
} else {
|
} else {
|
||||||
indexAliasesService.indicesAliases(
|
indexAliasesService.indicesAliases(
|
||||||
prepareIndicesAliasesRequest(sourceIndexName, rolloverIndexName, rolloverRequest),
|
prepareIndicesAliasesRequest(sourceIndexName, rolloverIndexName, rolloverRequest),
|
||||||
new IndicesAliasesListener(sourceIndexName, rolloverIndexName, listener));
|
new IndicesAliasesListener(rolloverResponse, listener));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// conditions not met
|
// conditions not met
|
||||||
listener.onResponse(new RolloverResponse(sourceIndexName, sourceIndexName));
|
listener.onResponse(
|
||||||
|
new RolloverResponse(sourceIndexName, sourceIndexName, conditionStatus, false, false,
|
||||||
|
false)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,18 +173,16 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
||||||
private static final class IndicesAliasesListener implements ActionListener<ClusterStateUpdateResponse> {
|
private static final class IndicesAliasesListener implements ActionListener<ClusterStateUpdateResponse> {
|
||||||
|
|
||||||
private final ActionListener<RolloverResponse> listener;
|
private final ActionListener<RolloverResponse> listener;
|
||||||
private final String oldIndex;
|
private final RolloverResponse response;
|
||||||
private final String newIndex;
|
|
||||||
|
|
||||||
public IndicesAliasesListener(String oldIndex, String newIndex, ActionListener<RolloverResponse> listener) {
|
public IndicesAliasesListener(RolloverResponse response, ActionListener<RolloverResponse> listener) {
|
||||||
this.oldIndex = oldIndex;
|
this.response = response;
|
||||||
this.newIndex = newIndex;
|
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
|
public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
|
||||||
listener.onResponse(new RolloverResponse(oldIndex, newIndex));
|
listener.onResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -199,24 +218,21 @@ public class TransportRolloverAction extends TransportMasterNodeAction<RolloverR
|
||||||
return String.join("-", indexPrefix, String.valueOf(counter));
|
return String.join("-", indexPrefix, String.valueOf(counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean satisfiesConditions(Set<Condition> conditions, long docCount, long indexAge) {
|
static Set<Map.Entry<Condition, Boolean>> evaluateConditions(Set<Condition> conditions, long docCount, long indexAge) {
|
||||||
|
Set<Map.Entry<Condition, Boolean>> result = new HashSet<>(conditions.size());
|
||||||
for (Condition condition: conditions) {
|
for (Condition condition: conditions) {
|
||||||
if (condition instanceof Condition.MaxAge) {
|
if (condition instanceof Condition.MaxAge) {
|
||||||
Condition.MaxAge maxAge = (Condition.MaxAge) condition;
|
Condition.MaxAge maxAge = (Condition.MaxAge) condition;
|
||||||
final TimeValue age = TimeValue.timeValueMillis(indexAge);
|
final TimeValue age = TimeValue.timeValueMillis(indexAge);
|
||||||
if (maxAge.matches(age) == false) {
|
result.add(new AbstractMap.SimpleEntry<>(condition, maxAge.matches(age)));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (condition instanceof Condition.MaxDocs) {
|
} else if (condition instanceof Condition.MaxDocs) {
|
||||||
final Condition.MaxDocs maxDocs = (Condition.MaxDocs) condition;
|
final Condition.MaxDocs maxDocs = (Condition.MaxDocs) condition;
|
||||||
if (maxDocs.matches(docCount) == false) {
|
result.add(new AbstractMap.SimpleEntry<>(condition, maxDocs.matches(docCount)));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("unknown condition [" + condition.getClass().getSimpleName() + "]");
|
throw new IllegalArgumentException("unknown condition [" + condition.getClass().getSimpleName() + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void validate(MetaData metaData, RolloverRequest request) {
|
static void validate(MetaData metaData, RolloverRequest request) {
|
||||||
|
|
|
@ -20,20 +20,14 @@
|
||||||
package org.elasticsearch.rest.action.admin.indices;
|
package org.elasticsearch.rest.action.admin.indices;
|
||||||
|
|
||||||
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
||||||
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
||||||
import org.elasticsearch.rest.BaseRestHandler;
|
import org.elasticsearch.rest.BaseRestHandler;
|
||||||
import org.elasticsearch.rest.BytesRestResponse;
|
|
||||||
import org.elasticsearch.rest.RestChannel;
|
import org.elasticsearch.rest.RestChannel;
|
||||||
import org.elasticsearch.rest.RestController;
|
import org.elasticsearch.rest.RestController;
|
||||||
import org.elasticsearch.rest.RestRequest;
|
import org.elasticsearch.rest.RestRequest;
|
||||||
import org.elasticsearch.rest.RestResponse;
|
import org.elasticsearch.rest.action.support.RestToXContentListener;
|
||||||
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
|
||||||
|
|
||||||
import static org.elasticsearch.rest.RestStatus.OK;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -45,6 +39,7 @@ public class RestRolloverIndexAction extends BaseRestHandler {
|
||||||
super(settings, client);
|
super(settings, client);
|
||||||
controller.registerHandler(RestRequest.Method.PUT, "/{alias}/_rollover", this);
|
controller.registerHandler(RestRequest.Method.PUT, "/{alias}/_rollover", this);
|
||||||
controller.registerHandler(RestRequest.Method.POST, "/{alias}/_rollover", this);
|
controller.registerHandler(RestRequest.Method.POST, "/{alias}/_rollover", this);
|
||||||
|
controller.registerHandler(RestRequest.Method.GET, "/{alias}/_rollover", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -56,17 +51,9 @@ public class RestRolloverIndexAction extends BaseRestHandler {
|
||||||
if (request.hasContent()) {
|
if (request.hasContent()) {
|
||||||
rolloverIndexRequest.source(request.content());
|
rolloverIndexRequest.source(request.content());
|
||||||
}
|
}
|
||||||
|
rolloverIndexRequest.simulate(request.method() == RestRequest.Method.GET || request.paramAsBoolean("simulate", false));
|
||||||
rolloverIndexRequest.timeout(request.paramAsTime("timeout", rolloverIndexRequest.timeout()));
|
rolloverIndexRequest.timeout(request.paramAsTime("timeout", rolloverIndexRequest.timeout()));
|
||||||
rolloverIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", rolloverIndexRequest.masterNodeTimeout()));
|
rolloverIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", rolloverIndexRequest.masterNodeTimeout()));
|
||||||
client.admin().indices().rolloverIndex(rolloverIndexRequest, new RestBuilderListener<RolloverResponse>(channel) {
|
client.admin().indices().rolloverIndex(rolloverIndexRequest, new RestToXContentListener<>(channel));
|
||||||
@Override
|
|
||||||
public RestResponse buildResponse(RolloverResponse rolloverResponse, XContentBuilder builder) throws Exception {
|
|
||||||
builder.startObject();
|
|
||||||
builder.field("old_index", rolloverResponse.getOldIndex());
|
|
||||||
builder.field("new_index", rolloverResponse.getNewIndex());
|
|
||||||
builder.endObject();
|
|
||||||
return new BytesRestResponse(OK, builder);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* 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.action.admin.indices.rollover;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
public class ConditionTests extends ESTestCase {
|
||||||
|
|
||||||
|
public void testMaxAge() throws Exception {
|
||||||
|
final Condition.MaxAge maxAge = new Condition.MaxAge(TimeValue.timeValueMillis(10));
|
||||||
|
assertThat(maxAge.matches(TimeValue.timeValueMillis(randomIntBetween(0, 9))), equalTo(false));
|
||||||
|
assertThat(maxAge.matches(TimeValue.timeValueMillis(randomIntBetween(10, 100))), equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMaxDocs() throws Exception {
|
||||||
|
final Condition.MaxDocs maxDocs = new Condition.MaxDocs(10L);
|
||||||
|
assertThat(maxDocs.matches((long) randomIntBetween(0, 9)), equalTo(false));
|
||||||
|
assertThat(maxDocs.matches((long) randomIntBetween(10, 100)), equalTo(true));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,6 +25,8 @@ import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.test.ESIntegTestCase;
|
import org.elasticsearch.test.ESIntegTestCase;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
@ -36,6 +38,10 @@ public class RolloverIT extends ESIntegTestCase {
|
||||||
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
||||||
assertThat(response.getOldIndex(), equalTo("test_index"));
|
assertThat(response.getOldIndex(), equalTo("test_index"));
|
||||||
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
||||||
|
assertThat(response.isSimulate(), equalTo(false));
|
||||||
|
assertThat(response.isRolledOver(), equalTo(true));
|
||||||
|
assertThat(response.isRolloverIndexCreated(), equalTo(true));
|
||||||
|
assertThat(response.getConditionStatus().size(), equalTo(0));
|
||||||
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||||
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
||||||
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
||||||
|
@ -50,6 +56,10 @@ public class RolloverIT extends ESIntegTestCase {
|
||||||
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
||||||
assertThat(response.getOldIndex(), equalTo("test_index"));
|
assertThat(response.getOldIndex(), equalTo("test_index"));
|
||||||
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
||||||
|
assertThat(response.isSimulate(), equalTo(false));
|
||||||
|
assertThat(response.isRolledOver(), equalTo(true));
|
||||||
|
assertThat(response.isRolloverIndexCreated(), equalTo(true));
|
||||||
|
assertThat(response.getConditionStatus().size(), equalTo(0));
|
||||||
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||||
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
||||||
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
||||||
|
@ -57,6 +67,24 @@ public class RolloverIT extends ESIntegTestCase {
|
||||||
assertTrue(newIndex.getAliases().containsKey("test_alias"));
|
assertTrue(newIndex.getAliases().containsKey("test_alias"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRolloverSimulate() throws Exception {
|
||||||
|
assertAcked(prepareCreate("test_index").addAlias(new Alias("test_alias")).get());
|
||||||
|
index("test_index", "type1", "1", "field", "value");
|
||||||
|
flush("test_index");
|
||||||
|
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").simulate(true).get();
|
||||||
|
assertThat(response.getOldIndex(), equalTo("test_index"));
|
||||||
|
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
||||||
|
assertThat(response.isSimulate(), equalTo(true));
|
||||||
|
assertThat(response.isRolledOver(), equalTo(false));
|
||||||
|
assertThat(response.isRolloverIndexCreated(), equalTo(false));
|
||||||
|
assertThat(response.getConditionStatus().size(), equalTo(0));
|
||||||
|
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||||
|
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
||||||
|
assertTrue(oldIndex.getAliases().containsKey("test_alias"));
|
||||||
|
final IndexMetaData newIndex = state.metaData().index("test_index-1");
|
||||||
|
assertNull(newIndex);
|
||||||
|
}
|
||||||
|
|
||||||
public void testRolloverConditionsNotMet() throws Exception {
|
public void testRolloverConditionsNotMet() throws Exception {
|
||||||
assertAcked(prepareCreate("test_index").addAlias(new Alias("test_alias")).get());
|
assertAcked(prepareCreate("test_index").addAlias(new Alias("test_alias")).get());
|
||||||
index("test_index", "type1", "1", "field", "value");
|
index("test_index", "type1", "1", "field", "value");
|
||||||
|
@ -65,6 +93,13 @@ public class RolloverIT extends ESIntegTestCase {
|
||||||
.addMaxIndexAgeCondition(TimeValue.timeValueHours(4)).get();
|
.addMaxIndexAgeCondition(TimeValue.timeValueHours(4)).get();
|
||||||
assertThat(response.getOldIndex(), equalTo("test_index"));
|
assertThat(response.getOldIndex(), equalTo("test_index"));
|
||||||
assertThat(response.getNewIndex(), equalTo("test_index"));
|
assertThat(response.getNewIndex(), equalTo("test_index"));
|
||||||
|
assertThat(response.isSimulate(), equalTo(false));
|
||||||
|
assertThat(response.isRolledOver(), equalTo(false));
|
||||||
|
assertThat(response.isRolloverIndexCreated(), equalTo(false));
|
||||||
|
assertThat(response.getConditionStatus().size(), equalTo(1));
|
||||||
|
final Map.Entry<String, Boolean> conditionEntry = response.getConditionStatus().iterator().next();
|
||||||
|
assertThat(conditionEntry.getKey(), equalTo(new Condition.MaxAge(TimeValue.timeValueHours(4)).toString()));
|
||||||
|
assertThat(conditionEntry.getValue(), equalTo(false));
|
||||||
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||||
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
||||||
assertTrue(oldIndex.getAliases().containsKey("test_alias"));
|
assertTrue(oldIndex.getAliases().containsKey("test_alias"));
|
||||||
|
@ -81,6 +116,9 @@ public class RolloverIT extends ESIntegTestCase {
|
||||||
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
|
||||||
assertThat(response.getOldIndex(), equalTo("test_index"));
|
assertThat(response.getOldIndex(), equalTo("test_index"));
|
||||||
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
assertThat(response.getNewIndex(), equalTo("test_index-1"));
|
||||||
|
assertThat(response.isSimulate(), equalTo(false));
|
||||||
|
assertThat(response.isRolledOver(), equalTo(true));
|
||||||
|
assertThat(response.isRolloverIndexCreated(), equalTo(false));
|
||||||
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
final ClusterState state = client().admin().cluster().prepareState().get().getState();
|
||||||
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
final IndexMetaData oldIndex = state.metaData().index("test_index");
|
||||||
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
|
||||||
|
|
|
@ -27,44 +27,13 @@ import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||||
import org.elasticsearch.cluster.metadata.MetaData;
|
import org.elasticsearch.cluster.metadata.MetaData;
|
||||||
import org.elasticsearch.common.UUIDs;
|
import org.elasticsearch.common.UUIDs;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
|
||||||
import org.elasticsearch.common.util.set.Sets;
|
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
|
|
||||||
public class TransportRolloverActionTests extends ESTestCase {
|
public class TransportRolloverActionTests extends ESTestCase {
|
||||||
|
|
||||||
public void testSatisfyConditions() throws Exception {
|
|
||||||
Set<Condition> conditions = Collections.emptySet();
|
|
||||||
assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomLong(),
|
|
||||||
randomLong()));
|
|
||||||
|
|
||||||
conditions = Collections.singleton(new Condition.MaxAge(TimeValue.timeValueMillis(10)));
|
|
||||||
assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomLong(),
|
|
||||||
TimeValue.timeValueMillis(randomIntBetween(10, 100)).getMillis()));
|
|
||||||
assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomLong(),
|
|
||||||
TimeValue.timeValueMillis(randomIntBetween(0, 9)).getMillis()));
|
|
||||||
|
|
||||||
conditions = Collections.singleton(new Condition.MaxDocs(10L));
|
|
||||||
assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(10, 100),
|
|
||||||
randomLong()));
|
|
||||||
assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1, 9),
|
|
||||||
randomLong()));
|
|
||||||
|
|
||||||
conditions = Sets.newHashSet(new Condition.MaxAge(TimeValue.timeValueMillis(100)), new Condition.MaxDocs(1000L));
|
|
||||||
assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1000, 1500),
|
|
||||||
TimeValue.timeValueMillis(randomIntBetween(100, 1000)).getMillis()));
|
|
||||||
assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1, 999),
|
|
||||||
TimeValue.timeValueMillis(randomIntBetween(100, 1000)).getMillis()));
|
|
||||||
assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1000, 1500),
|
|
||||||
TimeValue.timeValueMillis(randomIntBetween(0, 99)).getMillis()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCreateUpdateAliasRequest() throws Exception {
|
public void testCreateUpdateAliasRequest() throws Exception {
|
||||||
String sourceAlias = randomAsciiOfLength(10);
|
String sourceAlias = randomAsciiOfLength(10);
|
||||||
String sourceIndex = randomAsciiOfLength(10);
|
String sourceIndex = randomAsciiOfLength(10);
|
||||||
|
@ -114,35 +83,27 @@ public class TransportRolloverActionTests extends ESTestCase {
|
||||||
.putAlias(AliasMetaData.builder(aliasWithMultipleIndices))
|
.putAlias(AliasMetaData.builder(aliasWithMultipleIndices))
|
||||||
).build();
|
).build();
|
||||||
|
|
||||||
try {
|
expectThrows(IllegalArgumentException.class, () ->
|
||||||
TransportRolloverAction.validate(metaData, new RolloverRequest(aliasWithMultipleIndices));
|
TransportRolloverAction.validate(metaData, new RolloverRequest(aliasWithMultipleIndices)));
|
||||||
fail("expected to throw exception");
|
expectThrows(IllegalArgumentException.class, () ->
|
||||||
} catch (IllegalArgumentException e) {
|
TransportRolloverAction.validate(metaData, new RolloverRequest(randomFrom(index1, index2))));
|
||||||
assertThat(e.getMessage(), equalTo("source alias maps to multiple indices"));
|
expectThrows(IllegalArgumentException.class, () ->
|
||||||
}
|
TransportRolloverAction.validate(metaData, new RolloverRequest(randomAsciiOfLength(5)))
|
||||||
|
);
|
||||||
try {
|
|
||||||
TransportRolloverAction.validate(metaData, new RolloverRequest(randomFrom(index1, index2)));
|
|
||||||
fail("expected to throw exception");
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
assertThat(e.getMessage(), equalTo("source alias is a concrete index"));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
TransportRolloverAction.validate(metaData, new RolloverRequest(randomAsciiOfLength(5)));
|
|
||||||
fail("expected to throw exception");
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
assertThat(e.getMessage(), equalTo("source alias does not exist"));
|
|
||||||
}
|
|
||||||
|
|
||||||
TransportRolloverAction.validate(metaData, new RolloverRequest(alias));
|
TransportRolloverAction.validate(metaData, new RolloverRequest(alias));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGenerateRolloverIndexName() throws Exception {
|
public void testGenerateRolloverIndexName() throws Exception {
|
||||||
String index = randomAsciiOfLength(10);
|
String indexNotEndingInNumbers = randomAsciiOfLength(10) + "A";
|
||||||
assertThat(TransportRolloverAction.generateRolloverIndexName(index), not(equalTo(index)));
|
assertThat(TransportRolloverAction.generateRolloverIndexName(indexNotEndingInNumbers),
|
||||||
assertThat(TransportRolloverAction.generateRolloverIndexName("index"), equalTo("index-1"));
|
not(equalTo(indexNotEndingInNumbers)));
|
||||||
assertThat(TransportRolloverAction.generateRolloverIndexName("index-1"), equalTo("index-2"));
|
assertThat(TransportRolloverAction.generateRolloverIndexName(indexNotEndingInNumbers),
|
||||||
|
equalTo(indexNotEndingInNumbers + "-1"));
|
||||||
|
int num = randomIntBetween(0, 100);
|
||||||
|
final String indexPrefix = randomAsciiOfLength(10);
|
||||||
|
String indexEndingInNumbers = indexPrefix + "-" + num;
|
||||||
|
assertThat(TransportRolloverAction.generateRolloverIndexName(indexEndingInNumbers),
|
||||||
|
equalTo(indexPrefix + "-" + (num + 1)));
|
||||||
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-2"));
|
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-2"));
|
||||||
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name"), equalTo("index-name-1"));
|
assertThat(TransportRolloverAction.generateRolloverIndexName("index-name"), equalTo("index-name-1"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue