|
| 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.http; |
| 21 | + |
| 22 | +import org.apache.http.client.methods.HttpGet; |
| 23 | +import org.elasticsearch.Version; |
| 24 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateAction; |
| 25 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 26 | +import org.elasticsearch.client.Cancellable; |
| 27 | +import org.elasticsearch.client.Request; |
| 28 | +import org.elasticsearch.client.Response; |
| 29 | +import org.elasticsearch.client.ResponseListener; |
| 30 | +import org.elasticsearch.cluster.AbstractDiffable; |
| 31 | +import org.elasticsearch.cluster.ClusterState; |
| 32 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
| 33 | +import org.elasticsearch.cluster.service.ClusterService; |
| 34 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 35 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 36 | +import org.elasticsearch.common.util.CollectionUtils; |
| 37 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 38 | +import org.elasticsearch.plugins.Plugin; |
| 39 | +import org.elasticsearch.tasks.TaskInfo; |
| 40 | + |
| 41 | +import java.util.Collection; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.List; |
| 44 | +import java.util.concurrent.CancellationException; |
| 45 | +import java.util.function.UnaryOperator; |
| 46 | + |
| 47 | +public class ClusterStateRestCancellationIT extends HttpSmokeTestCase { |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 51 | + return CollectionUtils.appendToCopy(super.nodePlugins(), AssertingCustomPlugin.class); |
| 52 | + } |
| 53 | + |
| 54 | + private void updateClusterState(ClusterService clusterService, UnaryOperator<ClusterState> updateOperator) { |
| 55 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 56 | + clusterService.submitStateUpdateTask("update state", new ClusterStateUpdateTask() { |
| 57 | + @Override |
| 58 | + public ClusterState execute(ClusterState currentState) { |
| 59 | + return updateOperator.apply(currentState); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onFailure(String source, Exception e) { |
| 64 | + throw new AssertionError(source, e); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { |
| 69 | + future.onResponse(null); |
| 70 | + } |
| 71 | + }); |
| 72 | + future.actionGet(); |
| 73 | + } |
| 74 | + |
| 75 | + public void testClusterStateRestCancellation() throws Exception { |
| 76 | + |
| 77 | + final ClusterService clusterService = internalCluster().getInstance(ClusterService.class, internalCluster().getMasterName()); |
| 78 | + updateClusterState(clusterService, s -> ClusterState.builder(s).putCustom(AssertingCustom.NAME, AssertingCustom.INSTANCE).build()); |
| 79 | + |
| 80 | + final Request clusterStateRequest = new Request(HttpGet.METHOD_NAME, "/_cluster/state"); |
| 81 | + clusterStateRequest.addParameter("wait_for_metadata_version", Long.toString(Long.MAX_VALUE)); |
| 82 | + clusterStateRequest.addParameter("wait_for_timeout", "1h"); |
| 83 | + |
| 84 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 85 | + logger.info("--> sending cluster state request"); |
| 86 | + final Cancellable cancellable = getRestClient().performRequestAsync(clusterStateRequest, new ResponseListener() { |
| 87 | + @Override |
| 88 | + public void onSuccess(Response response) { |
| 89 | + future.onResponse(null); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onFailure(Exception exception) { |
| 94 | + future.onFailure(exception); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + logger.info("--> waiting for task to start"); |
| 99 | + assertBusy(() -> { |
| 100 | + final List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().get().getTasks(); |
| 101 | + assertTrue(tasks.toString(), tasks.stream().anyMatch(t -> t.getAction().equals(ClusterStateAction.NAME))); |
| 102 | + }); |
| 103 | + |
| 104 | + logger.info("--> cancelling cluster state request"); |
| 105 | + cancellable.cancel(); |
| 106 | + expectThrows(CancellationException.class, future::actionGet); |
| 107 | + |
| 108 | + logger.info("--> checking cluster state task completed"); |
| 109 | + assertBusy(() -> { |
| 110 | + updateClusterState(clusterService, s -> ClusterState.builder(s).build()); |
| 111 | + final List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().get().getTasks(); |
| 112 | + assertTrue(tasks.toString(), tasks.stream().noneMatch(t -> t.getAction().equals(ClusterStateAction.NAME))); |
| 113 | + }); |
| 114 | + |
| 115 | + updateClusterState(clusterService, s -> ClusterState.builder(s).removeCustom(AssertingCustom.NAME).build()); |
| 116 | + } |
| 117 | + |
| 118 | + private static class AssertingCustom extends AbstractDiffable<ClusterState.Custom> implements ClusterState.Custom { |
| 119 | + |
| 120 | + static final String NAME = "asserting"; |
| 121 | + static final AssertingCustom INSTANCE = new AssertingCustom(); |
| 122 | + |
| 123 | + @Override |
| 124 | + public String getWriteableName() { |
| 125 | + return NAME; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Version getMinimalSupportedVersion() { |
| 130 | + return Version.CURRENT; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void writeTo(StreamOutput out) { |
| 135 | + // no content |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) { |
| 140 | + throw new AssertionError("task should have been cancelled before serializing this custom"); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static class AssertingCustomPlugin extends Plugin { |
| 145 | + @Override |
| 146 | + public List<NamedWriteableRegistry.Entry> getNamedWriteables() { |
| 147 | + return Collections.singletonList( |
| 148 | + new NamedWriteableRegistry.Entry(ClusterState.Custom.class, AssertingCustom.NAME, in -> AssertingCustom.INSTANCE)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | +} |
0 commit comments