|
| 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 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.http; |
| 10 | + |
| 11 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 12 | +import org.elasticsearch.client.Cancellable; |
| 13 | +import org.elasticsearch.client.Request; |
| 14 | +import org.elasticsearch.client.Response; |
| 15 | +import org.elasticsearch.client.ResponseListener; |
| 16 | +import org.elasticsearch.common.lease.Releasable; |
| 17 | +import org.elasticsearch.common.lease.Releasables; |
| 18 | +import org.elasticsearch.common.settings.Setting; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.common.util.CollectionUtils; |
| 21 | +import org.elasticsearch.index.IndexService; |
| 22 | +import org.elasticsearch.index.IndexSettings; |
| 23 | +import org.elasticsearch.index.engine.Engine; |
| 24 | +import org.elasticsearch.index.engine.EngineConfig; |
| 25 | +import org.elasticsearch.index.engine.EngineException; |
| 26 | +import org.elasticsearch.index.engine.EngineFactory; |
| 27 | +import org.elasticsearch.index.engine.ReadOnlyEngine; |
| 28 | +import org.elasticsearch.index.shard.IndexShard; |
| 29 | +import org.elasticsearch.index.shard.IndexShardTestCase; |
| 30 | +import org.elasticsearch.index.translog.TranslogStats; |
| 31 | +import org.elasticsearch.indices.IndicesService; |
| 32 | +import org.elasticsearch.plugins.EnginePlugin; |
| 33 | +import org.elasticsearch.plugins.Plugin; |
| 34 | +import org.elasticsearch.tasks.CancellableTask; |
| 35 | +import org.elasticsearch.tasks.TaskInfo; |
| 36 | +import org.elasticsearch.transport.TransportService; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Optional; |
| 42 | +import java.util.concurrent.CancellationException; |
| 43 | +import java.util.concurrent.Semaphore; |
| 44 | +import java.util.function.Function; |
| 45 | + |
| 46 | +import static java.util.Collections.singletonList; |
| 47 | +import static org.hamcrest.Matchers.empty; |
| 48 | +import static org.hamcrest.Matchers.not; |
| 49 | + |
| 50 | +/** |
| 51 | + * Base class for testing that cancellation works at the REST layer for requests that need to acquire a searcher on one or more shards. |
| 52 | + * |
| 53 | + * It works by blocking searcher acquisition in order to catch the request mid-execution, and then to check that all the tasks are cancelled |
| 54 | + * before they complete normally. |
| 55 | + */ |
| 56 | +public abstract class BlockedSearcherRestCancellationTestCase extends HttpSmokeTestCase { |
| 57 | + |
| 58 | + private static final Setting<Boolean> BLOCK_SEARCHER_SETTING |
| 59 | + = Setting.boolSetting("index.block_searcher", false, Setting.Property.IndexScope); |
| 60 | + |
| 61 | + @Override |
| 62 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 63 | + return CollectionUtils.appendToCopy(super.nodePlugins(), SearcherBlockingPlugin.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected boolean addMockInternalEngine() { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + void runTest(Request request, String actionPrefix) throws Exception { |
| 72 | + |
| 73 | + createIndex("test", Settings.builder().put(BLOCK_SEARCHER_SETTING.getKey(), true).build()); |
| 74 | + ensureGreen("test"); |
| 75 | + |
| 76 | + final List<Semaphore> searcherBlocks = new ArrayList<>(); |
| 77 | + for (final IndicesService indicesService : internalCluster().getInstances(IndicesService.class)) { |
| 78 | + for (final IndexService indexService : indicesService) { |
| 79 | + for (final IndexShard indexShard : indexService) { |
| 80 | + final Engine engine = IndexShardTestCase.getEngine(indexShard); |
| 81 | + if (engine instanceof SearcherBlockingEngine) { |
| 82 | + searcherBlocks.add(((SearcherBlockingEngine) engine).searcherBlock); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + assertThat(searcherBlocks, not(empty())); |
| 88 | + |
| 89 | + final List<Releasable> releasables = new ArrayList<>(); |
| 90 | + try { |
| 91 | + for (final Semaphore searcherBlock : searcherBlocks) { |
| 92 | + searcherBlock.acquire(); |
| 93 | + releasables.add(searcherBlock::release); |
| 94 | + } |
| 95 | + |
| 96 | + final PlainActionFuture<Void> future = new PlainActionFuture<>(); |
| 97 | + logger.info("--> sending request"); |
| 98 | + final Cancellable cancellable = getRestClient().performRequestAsync(request, new ResponseListener() { |
| 99 | + @Override |
| 100 | + public void onSuccess(Response response) { |
| 101 | + future.onResponse(null); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onFailure(Exception exception) { |
| 106 | + future.onFailure(exception); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + logger.info("--> waiting for task to start"); |
| 111 | + assertBusy(() -> { |
| 112 | + final List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().get().getTasks(); |
| 113 | + assertTrue(tasks.toString(), tasks.stream().anyMatch(t -> t.getAction().startsWith(actionPrefix))); |
| 114 | + }); |
| 115 | + |
| 116 | + logger.info("--> waiting for at least one task to hit a block"); |
| 117 | + assertBusy(() -> assertTrue(searcherBlocks.stream().anyMatch(Semaphore::hasQueuedThreads))); |
| 118 | + |
| 119 | + logger.info("--> cancelling request"); |
| 120 | + cancellable.cancel(); |
| 121 | + expectThrows(CancellationException.class, future::actionGet); |
| 122 | + |
| 123 | + logger.info("--> checking that all tasks are marked as cancelled"); |
| 124 | + assertBusy(() -> { |
| 125 | + boolean foundTask = false; |
| 126 | + for (TransportService transportService : internalCluster().getInstances(TransportService.class)) { |
| 127 | + for (CancellableTask cancellableTask : transportService.getTaskManager().getCancellableTasks().values()) { |
| 128 | + if (cancellableTask.getAction().startsWith(actionPrefix)) { |
| 129 | + foundTask = true; |
| 130 | + assertTrue( |
| 131 | + "task " + cancellableTask.getId() + "/" + cancellableTask.getAction() + " not cancelled", |
| 132 | + cancellableTask.isCancelled()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + assertTrue("found no cancellable tasks", foundTask); |
| 137 | + }); |
| 138 | + } finally { |
| 139 | + Releasables.close(releasables); |
| 140 | + } |
| 141 | + |
| 142 | + logger.info("--> checking that all tasks have finished"); |
| 143 | + assertBusy(() -> { |
| 144 | + final List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().get().getTasks(); |
| 145 | + assertTrue(tasks.toString(), tasks.stream().noneMatch(t -> t.getAction().startsWith(actionPrefix))); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + public static class SearcherBlockingPlugin extends Plugin implements EnginePlugin { |
| 150 | + |
| 151 | + @Override |
| 152 | + public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) { |
| 153 | + if (BLOCK_SEARCHER_SETTING.get(indexSettings.getSettings())) { |
| 154 | + return Optional.of(SearcherBlockingEngine::new); |
| 155 | + } |
| 156 | + return Optional.empty(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public List<Setting<?>> getSettings() { |
| 161 | + return singletonList(BLOCK_SEARCHER_SETTING); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static class SearcherBlockingEngine extends ReadOnlyEngine { |
| 166 | + |
| 167 | + final Semaphore searcherBlock = new Semaphore(1); |
| 168 | + |
| 169 | + SearcherBlockingEngine(EngineConfig config) { |
| 170 | + super(config, null, new TranslogStats(), true, Function.identity(), true, false); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public Searcher acquireSearcher(String source, SearcherScope scope, Function<Searcher, Searcher> wrapper) throws EngineException { |
| 175 | + try { |
| 176 | + searcherBlock.acquire(); |
| 177 | + } catch (InterruptedException e) { |
| 178 | + throw new AssertionError(e); |
| 179 | + } |
| 180 | + searcherBlock.release(); |
| 181 | + return super.acquireSearcher(source, scope, wrapper); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments