|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.retention_lease_bwc; |
| 21 | + |
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; |
| 24 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 25 | +import org.elasticsearch.action.support.GroupedActionListener; |
| 26 | +import org.elasticsearch.client.node.NodeClient; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 28 | +import org.elasticsearch.common.settings.Settings; |
| 29 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 30 | +import org.elasticsearch.index.seqno.RetentionLeaseActions; |
| 31 | +import org.elasticsearch.index.shard.ShardId; |
| 32 | +import org.elasticsearch.rest.BaseRestHandler; |
| 33 | +import org.elasticsearch.rest.BytesRestResponse; |
| 34 | +import org.elasticsearch.rest.RestController; |
| 35 | +import org.elasticsearch.rest.RestRequest; |
| 36 | +import org.elasticsearch.rest.RestStatus; |
| 37 | +import org.elasticsearch.rest.action.RestActionListener; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.UncheckedIOException; |
| 41 | +import java.util.Collection; |
| 42 | +import java.util.Collections; |
| 43 | + |
| 44 | +public class RestAddRetentionLeaseHandler extends BaseRestHandler { |
| 45 | + |
| 46 | + public RestAddRetentionLeaseHandler(final Settings settings, final RestController restController) { |
| 47 | + super(settings); |
| 48 | + restController.registerHandler(RestRequest.Method.PUT, "/{index}/_add_retention_lease", this); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getName() { |
| 53 | + return "add_retention_lease"; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { |
| 58 | + final String index = request.param("index"); |
| 59 | + final String id = request.param("id"); |
| 60 | + final long retainingSequenceNumber = Long.parseLong(request.param("retaining_sequence_number")); |
| 61 | + final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); |
| 62 | + clusterStateRequest.clear(); |
| 63 | + clusterStateRequest.metaData(true); |
| 64 | + clusterStateRequest.indices(index); |
| 65 | + return channel -> |
| 66 | + client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() { |
| 67 | + @Override |
| 68 | + public void onResponse(final ClusterStateResponse clusterStateResponse) { |
| 69 | + final IndexMetaData indexMetaData = clusterStateResponse.getState().metaData().index(index); |
| 70 | + final int numberOfShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexMetaData.getSettings()); |
| 71 | + |
| 72 | + final GroupedActionListener<RetentionLeaseActions.Response> listener = new GroupedActionListener<>( |
| 73 | + new RestActionListener<Collection<RetentionLeaseActions.Response>>(channel) { |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void processResponse( |
| 77 | + final Collection<RetentionLeaseActions.Response> responses) throws Exception { |
| 78 | + final XContentBuilder builder = channel.newBuilder().startObject().endObject(); |
| 79 | + channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); |
| 80 | + } |
| 81 | + |
| 82 | + }, |
| 83 | + numberOfShards, |
| 84 | + Collections.emptyList()); |
| 85 | + for (int i = 0; i < numberOfShards; i++) { |
| 86 | + final ShardId shardId = new ShardId(indexMetaData.getIndex(), i); |
| 87 | + client.execute( |
| 88 | + RetentionLeaseActions.Add.INSTANCE, |
| 89 | + new RetentionLeaseActions.AddRequest(shardId, id, retainingSequenceNumber, "rest"), |
| 90 | + listener); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onFailure(final Exception e) { |
| 96 | + try { |
| 97 | + channel.sendResponse(new BytesRestResponse(channel, RestStatus.SERVICE_UNAVAILABLE, e)); |
| 98 | + } catch (IOException inner) { |
| 99 | + inner.addSuppressed(e); |
| 100 | + throw new UncheckedIOException(inner); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments