|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.autoscaling.action; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.action.support.ActionFilters; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.action.support.master.TransportMasterNodeAction; |
| 15 | +import org.elasticsearch.cluster.AckedClusterStateUpdateTask; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
| 17 | +import org.elasticsearch.cluster.block.ClusterBlockException; |
| 18 | +import org.elasticsearch.cluster.block.ClusterBlockLevel; |
| 19 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 20 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 21 | +import org.elasticsearch.cluster.service.ClusterService; |
| 22 | +import org.elasticsearch.common.inject.Inject; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.tasks.Task; |
| 25 | +import org.elasticsearch.threadpool.ThreadPool; |
| 26 | +import org.elasticsearch.transport.TransportService; |
| 27 | +import org.elasticsearch.xpack.autoscaling.AutoscalingMetadata; |
| 28 | +import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicyMetadata; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.SortedMap; |
| 32 | +import java.util.TreeMap; |
| 33 | + |
| 34 | +public class TransportDeleteAutoscalingPolicyAction extends TransportMasterNodeAction< |
| 35 | + DeleteAutoscalingPolicyAction.Request, |
| 36 | + AcknowledgedResponse> { |
| 37 | + |
| 38 | + private static final Logger logger = LogManager.getLogger(TransportPutAutoscalingPolicyAction.class); |
| 39 | + |
| 40 | + @Inject |
| 41 | + public TransportDeleteAutoscalingPolicyAction( |
| 42 | + final TransportService transportService, |
| 43 | + final ClusterService clusterService, |
| 44 | + final ThreadPool threadPool, |
| 45 | + final ActionFilters actionFilters, |
| 46 | + final IndexNameExpressionResolver indexNameExpressionResolver |
| 47 | + ) { |
| 48 | + super( |
| 49 | + DeleteAutoscalingPolicyAction.NAME, |
| 50 | + transportService, |
| 51 | + clusterService, |
| 52 | + threadPool, |
| 53 | + actionFilters, |
| 54 | + DeleteAutoscalingPolicyAction.Request::new, |
| 55 | + indexNameExpressionResolver |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected String executor() { |
| 61 | + return ThreadPool.Names.SAME; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected AcknowledgedResponse read(final StreamInput in) throws IOException { |
| 66 | + return new AcknowledgedResponse(in); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void masterOperation( |
| 71 | + final Task task, |
| 72 | + final DeleteAutoscalingPolicyAction.Request request, |
| 73 | + final ClusterState state, |
| 74 | + final ActionListener<AcknowledgedResponse> listener |
| 75 | + ) { |
| 76 | + clusterService.submitStateUpdateTask("delete-autoscaling-policy", new AckedClusterStateUpdateTask<>(request, listener) { |
| 77 | + |
| 78 | + @Override |
| 79 | + protected AcknowledgedResponse newResponse(final boolean acknowledged) { |
| 80 | + return new AcknowledgedResponse(acknowledged); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ClusterState execute(final ClusterState currentState) { |
| 85 | + return deleteAutoscalingPolicy(currentState, request.name(), logger); |
| 86 | + } |
| 87 | + |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected ClusterBlockException checkBlock(final DeleteAutoscalingPolicyAction.Request request, final ClusterState state) { |
| 93 | + return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); |
| 94 | + } |
| 95 | + |
| 96 | + static ClusterState deleteAutoscalingPolicy(final ClusterState currentState, final String name, final Logger logger) { |
| 97 | + final ClusterState.Builder builder = ClusterState.builder(currentState); |
| 98 | + final AutoscalingMetadata currentMetadata; |
| 99 | + if (currentState.metadata().custom(AutoscalingMetadata.NAME) != null) { |
| 100 | + currentMetadata = currentState.metadata().custom(AutoscalingMetadata.NAME); |
| 101 | + } else { |
| 102 | + // we will reject the request below when we try to look up the policy by name |
| 103 | + currentMetadata = AutoscalingMetadata.EMPTY; |
| 104 | + } |
| 105 | + if (currentMetadata.policies().containsKey(name) == false) { |
| 106 | + throw new IllegalArgumentException("autoscaling policy with name [" + name + "] does not exist"); |
| 107 | + } |
| 108 | + final SortedMap<String, AutoscalingPolicyMetadata> newPolicies = new TreeMap<>(currentMetadata.policies()); |
| 109 | + final AutoscalingPolicyMetadata policy = newPolicies.remove(name); |
| 110 | + assert policy != null : name; |
| 111 | + logger.info("deleting autoscaling policy [{}]", name); |
| 112 | + final AutoscalingMetadata newMetadata = new AutoscalingMetadata(newPolicies); |
| 113 | + builder.metadata(Metadata.builder(currentState.getMetadata()).putCustom(AutoscalingMetadata.NAME, newMetadata).build()); |
| 114 | + return builder.build(); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments