|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.tiering; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse; |
| 14 | +import org.opensearch.action.admin.indices.stats.IndicesStatsResponse; |
| 15 | +import org.opensearch.action.support.ActionFilters; |
| 16 | +import org.opensearch.action.support.clustermanager.TransportClusterManagerNodeAction; |
| 17 | +import org.opensearch.action.support.master.AcknowledgedResponse; |
| 18 | +import org.opensearch.client.Client; |
| 19 | +import org.opensearch.cluster.ClusterState; |
| 20 | +import org.opensearch.cluster.block.ClusterBlockException; |
| 21 | +import org.opensearch.cluster.block.ClusterBlockLevel; |
| 22 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 23 | +import org.opensearch.cluster.service.ClusterService; |
| 24 | +import org.opensearch.common.inject.Inject; |
| 25 | +import org.opensearch.core.action.ActionListener; |
| 26 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 27 | +import org.opensearch.threadpool.ThreadPool; |
| 28 | +import org.opensearch.tiering.TieringClusterStateListener; |
| 29 | +import org.opensearch.tiering.TieringService; |
| 30 | +import org.opensearch.transport.TransportService; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +import static org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest.Metric.FS; |
| 35 | + |
| 36 | +/** |
| 37 | + * Transport Tiering API class to move index from hot to warm |
| 38 | + * |
| 39 | + * @opensearch.experimental |
| 40 | + */ |
| 41 | +public class TransportWarmTieringAction extends TransportClusterManagerNodeAction<TieringIndexRequest, AcknowledgedResponse> { |
| 42 | + |
| 43 | + private static final Logger logger = LogManager.getLogger(TransportWarmTieringAction.class); |
| 44 | + private final TieringService tieringService; |
| 45 | + private final Client client; |
| 46 | + @Inject |
| 47 | + public TransportWarmTieringAction(TransportService transportService, ClusterService clusterService, |
| 48 | + ThreadPool threadPool, TieringService tieringService, |
| 49 | + ActionFilters actionFilters, |
| 50 | + IndexNameExpressionResolver indexNameExpressionResolver, Client client) { |
| 51 | + super(WarmTieringAction.NAME, transportService, clusterService, threadPool, actionFilters, |
| 52 | + TieringIndexRequest::new, indexNameExpressionResolver); |
| 53 | + this.client = client; |
| 54 | + this.tieringService = tieringService; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected String executor() { |
| 59 | + return ThreadPool.Names.SAME; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected AcknowledgedResponse read(StreamInput in) throws IOException { |
| 64 | + return new AcknowledgedResponse(in); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected ClusterBlockException checkBlock(TieringIndexRequest request, ClusterState state) { |
| 69 | + ClusterBlockException blockException = |
| 70 | + state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); |
| 71 | + if (blockException == null) { |
| 72 | + // Check indices level block |
| 73 | + blockException = state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, |
| 74 | + indexNameExpressionResolver.concreteIndexNames(clusterService.state(), request)); |
| 75 | + } |
| 76 | + return blockException; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void clusterManagerOperation(TieringIndexRequest request, ClusterState state, |
| 81 | + ActionListener<AcknowledgedResponse> listener) throws Exception { |
| 82 | + // Collect node stats to get node level filesystem info. The response will be used for performing some |
| 83 | + // validations. |
| 84 | + client.admin().cluster().prepareNodesStats().clear().addMetric(FS.metricName()).execute( |
| 85 | + new ActionListener<NodesStatsResponse>() { |
| 86 | + @Override |
| 87 | + public void onResponse(NodesStatsResponse nodesStatsResponse) { |
| 88 | + // Collect index level stats. This response is also used for validations. |
| 89 | + client.admin().indices().prepareStats().clear().setStore(true).setIndices(request.indices()).execute( |
| 90 | + new ActionListener<IndicesStatsResponse>() { |
| 91 | + @Override |
| 92 | + public void onResponse(IndicesStatsResponse indicesStatsResponse) { |
| 93 | + tieringService.tier(request, nodesStatsResponse, indicesStatsResponse, |
| 94 | + ActionListener.delegateFailure(listener, (delegatedListener, acknowledgedResponse) -> { |
| 95 | + if (request.waitForCompletion()) { |
| 96 | + TieringClusterStateListener.createAndRegisterListener( |
| 97 | + clusterService, |
| 98 | + new AcknowledgedResponse(acknowledgedResponse.isAcknowledged()), |
| 99 | + delegatedListener |
| 100 | + ); |
| 101 | + } else { |
| 102 | + delegatedListener.onResponse(new AcknowledgedResponse(true)); |
| 103 | + } |
| 104 | + })); |
| 105 | + } |
| 106 | + @Override |
| 107 | + public void onFailure(Exception e) { |
| 108 | + logger.debug("Indices stats call failed with exception", e); |
| 109 | + listener.onFailure(e); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + @Override |
| 114 | + public void onFailure(Exception e) { |
| 115 | + logger.debug("Node stats call failed with exception", e); |
| 116 | + listener.onFailure(e); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments