|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.persistent; |
| 11 | + |
| 12 | +import org.elasticsearch.TransportVersion; |
| 13 | +import org.elasticsearch.client.internal.Client; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.ClusterStateUpdateTask; |
| 16 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 17 | +import org.elasticsearch.cluster.service.ClusterService; |
| 18 | +import org.elasticsearch.common.Priority; |
| 19 | +import org.elasticsearch.common.UUIDs; |
| 20 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 21 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 22 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 23 | +import org.elasticsearch.common.settings.SettingsModule; |
| 24 | +import org.elasticsearch.plugins.PersistentTaskPlugin; |
| 25 | +import org.elasticsearch.plugins.Plugin; |
| 26 | +import org.elasticsearch.plugins.PluginsService; |
| 27 | +import org.elasticsearch.tasks.TaskId; |
| 28 | +import org.elasticsearch.test.ClusterServiceUtils; |
| 29 | +import org.elasticsearch.test.ESIntegTestCase; |
| 30 | +import org.elasticsearch.threadpool.ThreadPool; |
| 31 | +import org.elasticsearch.xcontent.NamedXContentRegistry; |
| 32 | +import org.elasticsearch.xcontent.ParseField; |
| 33 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 40 | +import java.util.stream.StreamSupport; |
| 41 | + |
| 42 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 43 | + |
| 44 | +public class PersistentTaskCreationFailureIT extends ESIntegTestCase { |
| 45 | + @Override |
| 46 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 47 | + return List.of(FailingCreationPersistentTasksPlugin.class); |
| 48 | + } |
| 49 | + |
| 50 | + private static boolean hasPersistentTask(ClusterState clusterState) { |
| 51 | + return findTasks(clusterState, FailingCreationPersistentTaskExecutor.TASK_NAME).isEmpty() == false; |
| 52 | + } |
| 53 | + |
| 54 | + public void testPersistentTasksThatFailDuringCreationAreRemovedFromClusterState() { |
| 55 | + |
| 56 | + final var masterClusterService = internalCluster().getCurrentMasterNodeInstance(ClusterService.class); |
| 57 | + final var plugins = StreamSupport.stream(internalCluster().getInstances(PluginsService.class).spliterator(), false) |
| 58 | + .flatMap(ps -> ps.filterPlugins(FailingCreationPersistentTasksPlugin.class)) |
| 59 | + .toList(); |
| 60 | + plugins.forEach(plugin -> plugin.hasFailedToCreateTask.set(false)); |
| 61 | + |
| 62 | + final var taskCreatedListener = ClusterServiceUtils.addTemporaryStateListener( |
| 63 | + masterClusterService, |
| 64 | + PersistentTaskCreationFailureIT::hasPersistentTask |
| 65 | + ); |
| 66 | + |
| 67 | + taskCreatedListener.andThenAccept(v -> { |
| 68 | + // enqueue some higher-priority cluster state updates to check that they do not cause retries of the failing task creation step |
| 69 | + for (int i = 0; i < 5; i++) { |
| 70 | + masterClusterService.submitUnbatchedStateUpdateTask("test", new ClusterStateUpdateTask(Priority.IMMEDIATE) { |
| 71 | + @Override |
| 72 | + public ClusterState execute(ClusterState currentState) { |
| 73 | + assertTrue(hasPersistentTask(currentState)); |
| 74 | + |
| 75 | + assertTrue(waitUntil(() -> { |
| 76 | + final var completePersistentTaskPendingTasksCount = masterClusterService.getMasterService() |
| 77 | + .pendingTasks() |
| 78 | + .stream() |
| 79 | + .filter( |
| 80 | + pendingClusterTask -> pendingClusterTask.getSource().string().equals("finish persistent task (failed)") |
| 81 | + ) |
| 82 | + .count(); |
| 83 | + assertThat(completePersistentTaskPendingTasksCount, lessThanOrEqualTo(1L)); |
| 84 | + return completePersistentTaskPendingTasksCount == 1L; |
| 85 | + })); |
| 86 | + |
| 87 | + return currentState.copyAndUpdateMetadata( |
| 88 | + mdb -> mdb.putCustom( |
| 89 | + PersistentTasksCustomMetadata.TYPE, |
| 90 | + PersistentTasksCustomMetadata.builder( |
| 91 | + PersistentTasksCustomMetadata.getPersistentTasksCustomMetadata(currentState) |
| 92 | + ) |
| 93 | + // create and remove a fake task just to force a change in lastAllocationId so that |
| 94 | + // PersistentTasksNodeService checks for changes and potentially retries |
| 95 | + .addTask("test", "test", null, PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT) |
| 96 | + .removeTask("test") |
| 97 | + .build() |
| 98 | + ) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onFailure(Exception e) { |
| 104 | + fail(e); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + safeAwait( |
| 111 | + l -> internalCluster().getInstance(PersistentTasksService.class) |
| 112 | + .sendStartRequest( |
| 113 | + UUIDs.base64UUID(), |
| 114 | + FailingCreationPersistentTaskExecutor.TASK_NAME, |
| 115 | + new FailingCreationTaskParams(), |
| 116 | + null, |
| 117 | + l.map(ignored -> null) |
| 118 | + ) |
| 119 | + ); |
| 120 | + |
| 121 | + safeAwait( |
| 122 | + taskCreatedListener.<Void>andThen( |
| 123 | + (l, v) -> ClusterServiceUtils.addTemporaryStateListener( |
| 124 | + masterClusterService, |
| 125 | + clusterState -> hasPersistentTask(clusterState) == false |
| 126 | + ).addListener(l) |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + assertEquals(1L, plugins.stream().filter(plugin -> plugin.hasFailedToCreateTask.get()).count()); |
| 131 | + } |
| 132 | + |
| 133 | + public static class FailingCreationPersistentTasksPlugin extends Plugin implements PersistentTaskPlugin { |
| 134 | + |
| 135 | + private final AtomicBoolean hasFailedToCreateTask = new AtomicBoolean(); |
| 136 | + |
| 137 | + @Override |
| 138 | + public List<PersistentTasksExecutor<?>> getPersistentTasksExecutor( |
| 139 | + ClusterService clusterService, |
| 140 | + ThreadPool threadPool, |
| 141 | + Client client, |
| 142 | + SettingsModule settingsModule, |
| 143 | + IndexNameExpressionResolver expressionResolver |
| 144 | + ) { |
| 145 | + return List.of(new FailingCreationPersistentTaskExecutor(hasFailedToCreateTask)); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public List<NamedWriteableRegistry.Entry> getNamedWriteables() { |
| 150 | + return List.of( |
| 151 | + new NamedWriteableRegistry.Entry( |
| 152 | + PersistentTaskParams.class, |
| 153 | + FailingCreationPersistentTaskExecutor.TASK_NAME, |
| 154 | + FailingCreationTaskParams::new |
| 155 | + ) |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public List<NamedXContentRegistry.Entry> getNamedXContent() { |
| 161 | + return List.of( |
| 162 | + new NamedXContentRegistry.Entry( |
| 163 | + PersistentTaskParams.class, |
| 164 | + new ParseField(FailingCreationPersistentTaskExecutor.TASK_NAME), |
| 165 | + p -> { |
| 166 | + p.skipChildren(); |
| 167 | + return new FailingCreationTaskParams(); |
| 168 | + } |
| 169 | + ) |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public static class FailingCreationTaskParams implements PersistentTaskParams { |
| 175 | + public FailingCreationTaskParams() {} |
| 176 | + |
| 177 | + public FailingCreationTaskParams(StreamInput in) {} |
| 178 | + |
| 179 | + @Override |
| 180 | + public String getWriteableName() { |
| 181 | + return FailingCreationPersistentTaskExecutor.TASK_NAME; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public TransportVersion getMinimalSupportedVersion() { |
| 186 | + return TransportVersion.current(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void writeTo(StreamOutput out) throws IOException {} |
| 191 | + |
| 192 | + @Override |
| 193 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 194 | + builder.startObject(); |
| 195 | + builder.endObject(); |
| 196 | + return builder; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + static class FailingCreationPersistentTaskExecutor extends PersistentTasksExecutor<FailingCreationTaskParams> { |
| 201 | + static final String TASK_NAME = "cluster:admin/persistent/test_creation_failure"; |
| 202 | + |
| 203 | + private final AtomicBoolean hasFailedToCreateTask; |
| 204 | + |
| 205 | + FailingCreationPersistentTaskExecutor(AtomicBoolean hasFailedToCreateTask) { |
| 206 | + super(TASK_NAME, r -> fail("execution is unexpected")); |
| 207 | + this.hasFailedToCreateTask = hasFailedToCreateTask; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + protected AllocatedPersistentTask createTask( |
| 212 | + long id, |
| 213 | + String type, |
| 214 | + String action, |
| 215 | + TaskId parentTaskId, |
| 216 | + PersistentTasksCustomMetadata.PersistentTask<FailingCreationTaskParams> taskInProgress, |
| 217 | + Map<String, String> headers |
| 218 | + ) { |
| 219 | + assertTrue("already failed before", hasFailedToCreateTask.compareAndSet(false, true)); |
| 220 | + throw new RuntimeException("simulated"); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + protected void nodeOperation(AllocatedPersistentTask task, FailingCreationTaskParams params, PersistentTaskState state) { |
| 225 | + fail("execution is unexpected"); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments