Skip to content

HLRC: Add ILM Status to HLRC #33283

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

Merged
merged 8 commits into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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 @@ -22,6 +22,7 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
Expand Down Expand Up @@ -139,6 +140,34 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio
AcknowledgedResponse::fromXContent, emptySet());
}

/**
* Get the status of index lifecycle management
* See <a href="https://fix-me-when-we-have-docs.com">
* the docs</a> for more.
*
* @param request the request with user defined timeouts.
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public LifecycleManagementStatusResponse lifecycleManagementStatus(TimedRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::statusILM, options,
LifecycleManagementStatusResponse::fromXContent, emptySet());
}

/**
* Asynchronously get the status of index lifecycle management
* See <a href="https://fix-me-when-we-have-docs.com">
* the docs</a> for more.
*
* @param request the request with user defined timeouts.
* @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 lifecycleManagementStatusAsync(TimedRequest request, RequestOptions options,
ActionListener<LifecycleManagementStatusResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::statusILM, options,
LifecycleManagementStatusResponse::fromXContent, listener, emptySet());
}

/**
* Asynchronously stop the Index Lifecycle Management feature.
* See <a href="https://fix-me-when-we-have-docs.com">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,18 @@ static Request stopILM(StopILMRequest stopILMRequest) {
return request;
}

static Request statusILM(TimedRequest ilmStatusRequest){
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this lifecycleManagementStatus to match the method in the client?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks missed this one. done.

Request request = new Request(HttpGet.METHOD_NAME,
new EndpointBuilder()
.addPathPartAsIs("_ilm")
.addPathPartAsIs("status")
.build());
Params params = new Params(request);
params.withMasterTimeout(ilmStatusRequest.masterNodeTimeout());
params.withTimeout(ilmStatusRequest.timeout());
return request;
}

static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) {
String[] indices = explainLifecycleRequest.indices() == null ? Strings.EMPTY_ARRAY : explainLifecycleRequest.indices();
Request request = new Request(HttpGet.METHOD_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class TimedRequest implements Validatable {

public void setTimeout(TimeValue timeout) {
this.timeout = timeout;

}

public void setMasterTimeout(TimeValue masterTimeout) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.indexlifecycle;

import org.elasticsearch.action.admin.indices.shrink.ShrinkAction;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.util.EnumSet;
import java.util.Locale;
import java.util.Objects;

/**
* The current status of index lifecycle management. See {@link OperationMode} for available statuses.
*/
public class LifecycleManagementStatusResponse {

private final OperationMode operationMode;
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<LifecycleManagementStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
"operation_mode", a -> new LifecycleManagementStatusResponse((String) a[0]));

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("operation_mode"));
Copy link
Member

Choose a reason for hiding this comment

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

Can you put the ParseField into a class private var and then use it in the parser (so we don't accidentally typo/change it in the future)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved "operation_mode" to a static final string (not exactly the ask, but i believe same outcome)

}

//package private for testing
LifecycleManagementStatusResponse(String operationMode) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this should take an OperationMode to push the conversion into the caller, rather than doing it in the constructor (it seems cleaner to me that way).

This is just my personal preference though, so up to you if you want to change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leaving as-is

this.operationMode = OperationMode.fromString(operationMode);
}

public OperationMode getOperationMode() {
return operationMode;
}

public static LifecycleManagementStatusResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

/**
* Enum representing the different modes that Index Lifecycle Service can operate in.
*/
public enum OperationMode {
/**
* This represents a state where no policies are executed
*/
STOPPED,

/**
* this represents a state where only sensitive actions (like {@link ShrinkAction}) will be executed
* until they finish, at which point the operation mode will move to <code>STOPPED</code>.
*/
STOPPING,

/**
* Normal operation where all policies are executed as normal.
*/
RUNNING;

static OperationMode fromString(String string) {
return EnumSet.allOf(OperationMode.class).stream()
.filter(e -> string.equalsIgnoreCase(e.name())).findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ENGLISH, "%s is not a valid operation_mode", string)));
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LifecycleManagementStatusResponse that = (LifecycleManagementStatusResponse) o;
return operationMode == that.operationMode;
}

@Override
public int hashCode() {
return Objects.hash(operationMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
Expand Down Expand Up @@ -169,35 +169,33 @@ public void testStartStopILM() throws Exception {
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build());
createIndex("squash", Settings.EMPTY);

// TODO: NORELEASE convert this to using the high level client once
// there are APIs for it
Request statusReq = new Request("GET", "/_ilm/status");
Response statusResponse = client().performRequest(statusReq);
String statusResponseString = EntityUtils.toString(statusResponse.getEntity());
assertEquals("{\"operation_mode\":\"RUNNING\"}", statusResponseString);
TimedRequest statusRequest = new TimedRequest();
Copy link
Contributor

Choose a reason for hiding this comment

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

While I get the logic behind why you did it this way, I think generic classes make more sense for responses than requests. As a user of the HLRC, this feels really strange - even if GetILMStatusRequest would be an extra class that doesn't really add anything, I think having separate classes for each request type is the way to go. I could be wrong though, I'd like to hear others' input on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a strong opinion beyond consistency in the API.

Perhaps we should allow a no-arg method in the client that creates the request for them. However, that also creates inconsistency (unless you create a bunch of new methods).

I'd like to hear others' input on this.

..same

Copy link
Contributor

Choose a reason for hiding this comment

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

@hub-cap Could you weigh in on this?

LifecycleManagementStatusResponse statusResponse = execute(
statusRequest,
highLevelClient().indexLifecycle()::lifecycleManagementStatus,
highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync);
assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING);

StopILMRequest stopReq = new StopILMRequest();
AcknowledgedResponse stopResponse = execute(stopReq, highLevelClient().indexLifecycle()::stopILM,
highLevelClient().indexLifecycle()::stopILMAsync);
assertTrue(stopResponse.isAcknowledged());

// TODO: NORELEASE convert this to using the high level client once there are APIs for it
statusReq = new Request("GET", "/_ilm/status");
statusResponse = client().performRequest(statusReq);
statusResponseString = EntityUtils.toString(statusResponse.getEntity());
assertThat(statusResponseString,
Matchers.anyOf(equalTo("{\"operation_mode\":\"STOPPING\"}"), equalTo("{\"operation_mode\":\"STOPPED\"}")));

statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus,
highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync);
assertThat(statusResponse.getOperationMode(),
Matchers.anyOf(equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPING),
equalTo(LifecycleManagementStatusResponse.OperationMode.STOPPED)));

StartILMRequest startReq = new StartILMRequest();
AcknowledgedResponse startResponse = execute(startReq, highLevelClient().indexLifecycle()::startILM,
highLevelClient().indexLifecycle()::startILMAsync);
assertTrue(startResponse.isAcknowledged());

// TODO: NORELEASE convert this to using the high level client once there are APIs for it
statusReq = new Request("GET", "/_ilm/status");
statusResponse = client().performRequest(statusReq);
statusResponseString = EntityUtils.toString(statusResponse.getEntity());
assertEquals("{\"operation_mode\":\"RUNNING\"}", statusResponseString);
statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus,
highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync);
assertEquals(statusResponse.getOperationMode(), LifecycleManagementStatusResponse.OperationMode.RUNNING);
}

public void testExplainLifecycle() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,18 @@ public void testStopILM() throws Exception {
assertThat(request.getParameters(), equalTo(expectedParams));
}

public void testStatusILM() throws Exception {
TimedRequest req = new TimedRequest();
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams);

Request request = RequestConverters.statusILM(req);
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
assertThat(request.getEndpoint(), equalTo("/_ilm/status"));
assertThat(request.getParameters(), equalTo(expectedParams));
}

public void testExplainLifecycle() throws Exception {
ExplainLifecycleRequest req = new ExplainLifecycleRequest();
String[] indices = rarely() ? null : randomIndicesNames(0, 10);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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;

import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ESTestCase;

public class TimedRequestTests extends ESTestCase {

public void testDefaults() {
TimedRequest timedRequest = new TimedRequest();
assertEquals(timedRequest.timeout(), TimedRequest.DEFAULT_TIMEOUT);
assertEquals(timedRequest.masterNodeTimeout(), TimedRequest.DEFAULT_MASTER_TIMEOUT);
}

public void testNonDefaults() {
TimedRequest timedRequest = new TimedRequest();
TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000));
TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0,1000));
timedRequest.setTimeout(timeout);
timedRequest.setMasterTimeout(masterTimeout);
assertEquals(timedRequest.timeout(), timeout);
assertEquals(timedRequest.masterNodeTimeout(), masterTimeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.indexlifecycle;

import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.protocol.xpack.indexlifecycle.OperationMode;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.CoreMatchers;

import java.io.IOException;
import java.util.EnumSet;
import java.util.stream.Collectors;

public class LifecycleManagementStatusResponseTests extends ESTestCase {

public void testClientServerStatuses() {
assertEquals(
EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()),
EnumSet.allOf(OperationMode.class).stream().map(Enum::name).collect(Collectors.toSet()));
}

public void testFromName() {
EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class)
.forEach(e -> assertEquals(LifecycleManagementStatusResponse.OperationMode.fromString(e.name()), e));
}

public void testInvalidStatus() {
String invalidName = randomAlphaOfLength(10);
Exception e = expectThrows(IllegalArgumentException.class,
() -> LifecycleManagementStatusResponse.OperationMode.fromString(invalidName));
assertThat(e.getMessage(), CoreMatchers.containsString(invalidName + " is not a valid operation_mode"));
}

public void testValidStatuses() {
EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class)
.forEach(e -> assertEquals(new LifecycleManagementStatusResponse(e.name()).getOperationMode(), e));
}

public void testXContent() throws IOException {
XContentType xContentType = XContentType.JSON;
String mode = randomFrom(EnumSet.allOf(LifecycleManagementStatusResponse.OperationMode.class)
.stream().map(Enum::name).collect(Collectors.toList()));
XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}");
assertEquals(LifecycleManagementStatusResponse.fromXContent(parser).getOperationMode(),
LifecycleManagementStatusResponse.OperationMode.fromString(mode));
}
}