|
| 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.tasks; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.Level; |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.elasticsearch.Version; |
| 14 | +import org.elasticsearch.action.admin.cluster.node.tasks.TaskManagerTestCase; |
| 15 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 16 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 17 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 18 | +import org.elasticsearch.common.logging.Loggers; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.core.TimeValue; |
| 21 | +import org.elasticsearch.core.internal.io.IOUtils; |
| 22 | +import org.elasticsearch.test.MockLogAppender; |
| 23 | +import org.elasticsearch.test.junit.annotations.TestLogging; |
| 24 | +import org.elasticsearch.test.transport.MockTransportService; |
| 25 | +import org.elasticsearch.test.transport.StubbableTransport; |
| 26 | +import org.elasticsearch.threadpool.ThreadPool; |
| 27 | +import org.elasticsearch.transport.AbstractSimpleTransportTestCase; |
| 28 | +import org.elasticsearch.transport.NodeDisconnectedException; |
| 29 | +import org.elasticsearch.transport.TransportException; |
| 30 | +import org.elasticsearch.transport.TransportRequest; |
| 31 | +import org.elasticsearch.transport.TransportRequestOptions; |
| 32 | +import org.elasticsearch.transport.TransportResponse; |
| 33 | +import org.elasticsearch.transport.TransportResponseHandler; |
| 34 | + |
| 35 | +import java.io.Closeable; |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | +import java.util.function.Function; |
| 41 | + |
| 42 | +import static org.hamcrest.Matchers.anyOf; |
| 43 | +import static org.hamcrest.Matchers.instanceOf; |
| 44 | + |
| 45 | +public class BanFailureLoggingTests extends TaskManagerTestCase { |
| 46 | + |
| 47 | + @TestLogging(reason = "testing logging at DEBUG", value = "org.elasticsearch.tasks.TaskCancellationService:DEBUG") |
| 48 | + public void testLogsAtDebugOnDisconnectionDuringBan() throws Exception { |
| 49 | + runTest( |
| 50 | + (connection, requestId, action, request, options) -> { |
| 51 | + if (action.equals(TaskCancellationService.BAN_PARENT_ACTION_NAME)) { |
| 52 | + connection.close(); |
| 53 | + } |
| 54 | + connection.sendRequest(requestId, action, request, options); |
| 55 | + }, |
| 56 | + childNode -> new MockLogAppender.SeenEventExpectation( |
| 57 | + "cannot send message", |
| 58 | + TaskCancellationService.class.getName(), |
| 59 | + Level.DEBUG, |
| 60 | + "*cannot send ban for tasks*" + childNode.getId() + "*")); |
| 61 | + } |
| 62 | + |
| 63 | + @TestLogging(reason = "testing logging at DEBUG", value = "org.elasticsearch.tasks.TaskCancellationService:DEBUG") |
| 64 | + public void testLogsAtDebugOnDisconnectionDuringBanRemoval() throws Exception { |
| 65 | + final AtomicInteger banCount = new AtomicInteger(); |
| 66 | + runTest( |
| 67 | + (connection, requestId, action, request, options) -> { |
| 68 | + if (action.equals(TaskCancellationService.BAN_PARENT_ACTION_NAME) && banCount.incrementAndGet() >= 2) { |
| 69 | + connection.close(); |
| 70 | + } |
| 71 | + connection.sendRequest(requestId, action, request, options); |
| 72 | + }, |
| 73 | + childNode -> new MockLogAppender.SeenEventExpectation( |
| 74 | + "cannot send message", |
| 75 | + TaskCancellationService.class.getName(), |
| 76 | + Level.DEBUG, |
| 77 | + "*failed to remove ban for tasks*" + childNode.getId() + "*")); |
| 78 | + } |
| 79 | + |
| 80 | + private void runTest( |
| 81 | + StubbableTransport.SendRequestBehavior sendRequestBehavior, |
| 82 | + Function<DiscoveryNode, MockLogAppender.SeenEventExpectation> expectation) throws Exception { |
| 83 | + |
| 84 | + final ArrayList<Closeable> resources = new ArrayList<>(3); |
| 85 | + |
| 86 | + try { |
| 87 | + |
| 88 | + final MockTransportService parentTransportService = MockTransportService.createNewService( |
| 89 | + Settings.EMPTY, |
| 90 | + Version.CURRENT, |
| 91 | + threadPool); |
| 92 | + resources.add(parentTransportService); |
| 93 | + parentTransportService.getTaskManager().setTaskCancellationService(new TaskCancellationService(parentTransportService)); |
| 94 | + parentTransportService.start(); |
| 95 | + parentTransportService.acceptIncomingRequests(); |
| 96 | + |
| 97 | + final MockTransportService childTransportService = MockTransportService.createNewService( |
| 98 | + Settings.EMPTY, |
| 99 | + Version.CURRENT, |
| 100 | + threadPool); |
| 101 | + resources.add(childTransportService); |
| 102 | + childTransportService.getTaskManager().setTaskCancellationService(new TaskCancellationService(childTransportService)); |
| 103 | + childTransportService.registerRequestHandler( |
| 104 | + "internal:testAction[c]", |
| 105 | + ThreadPool.Names.MANAGEMENT, // busy-wait for cancellation but not on a transport thread |
| 106 | + (StreamInput in) -> new TransportRequest.Empty(in) { |
| 107 | + @Override |
| 108 | + public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { |
| 109 | + return new CancellableTask(id, type, action, "", parentTaskId, headers); |
| 110 | + } |
| 111 | + }, |
| 112 | + (request, channel, task) -> { |
| 113 | + final CancellableTask cancellableTask = (CancellableTask) task; |
| 114 | + assertBusy(() -> assertTrue(cancellableTask.isCancelled())); |
| 115 | + channel.sendResponse(new TaskCancelledException("task cancelled")); |
| 116 | + }); |
| 117 | + |
| 118 | + childTransportService.start(); |
| 119 | + childTransportService.acceptIncomingRequests(); |
| 120 | + |
| 121 | + parentTransportService.addSendBehavior(sendRequestBehavior); |
| 122 | + |
| 123 | + AbstractSimpleTransportTestCase.connectToNode(parentTransportService, childTransportService.getLocalDiscoNode()); |
| 124 | + |
| 125 | + final CancellableTask parentTask = (CancellableTask) parentTransportService.getTaskManager().register( |
| 126 | + "transport", |
| 127 | + "internal:testAction", |
| 128 | + new ParentRequest()); |
| 129 | + |
| 130 | + parentTransportService.sendChildRequest( |
| 131 | + childTransportService.getLocalDiscoNode(), |
| 132 | + "internal:testAction[c]", |
| 133 | + TransportRequest.Empty.INSTANCE, |
| 134 | + parentTask, |
| 135 | + TransportRequestOptions.EMPTY, |
| 136 | + new ChildResponseHandler(() -> parentTransportService.getTaskManager().unregister(parentTask))); |
| 137 | + |
| 138 | + MockLogAppender appender = new MockLogAppender(); |
| 139 | + appender.start(); |
| 140 | + resources.add(appender::stop); |
| 141 | + Loggers.addAppender(LogManager.getLogger(TaskCancellationService.class), appender); |
| 142 | + resources.add(() -> Loggers.removeAppender(LogManager.getLogger(TaskCancellationService.class), appender)); |
| 143 | + |
| 144 | + appender.addExpectation(expectation.apply(childTransportService.getLocalDiscoNode())); |
| 145 | + |
| 146 | + final PlainActionFuture<Void> cancellationFuture = new PlainActionFuture<>(); |
| 147 | + parentTransportService.getTaskManager().cancelTaskAndDescendants(parentTask, "test", true, cancellationFuture); |
| 148 | + try { |
| 149 | + cancellationFuture.actionGet(TimeValue.timeValueSeconds(5)); |
| 150 | + } catch (NodeDisconnectedException e) { |
| 151 | + // acceptable; we mostly ignore the result of cancellation anyway |
| 152 | + } |
| 153 | + |
| 154 | + // assert busy since failure to remove a ban may be logged after cancellation completed |
| 155 | + assertBusy(appender::assertAllExpectationsMatched); |
| 156 | + } finally { |
| 157 | + Collections.reverse(resources); |
| 158 | + IOUtils.close(resources); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static class ParentRequest implements TaskAwareRequest { |
| 163 | + @Override |
| 164 | + public void setParentTask(TaskId taskId) { |
| 165 | + fail("setParentTask should not be called"); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public TaskId getParentTask() { |
| 170 | + return TaskId.EMPTY_TASK_ID; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { |
| 175 | + return new CancellableTask(id, type, action, "", parentTaskId, headers); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static class ChildResponseHandler implements TransportResponseHandler<TransportResponse.Empty> { |
| 180 | + private final Runnable onException; |
| 181 | + |
| 182 | + ChildResponseHandler(Runnable onException) { |
| 183 | + this.onException = onException; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void handleResponse(TransportResponse.Empty response) { |
| 188 | + fail("should not get successful response"); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void handleException(TransportException exp) { |
| 193 | + assertThat(exp.unwrapCause(), anyOf( |
| 194 | + instanceOf(TaskCancelledException.class), |
| 195 | + instanceOf(NodeDisconnectedException.class))); |
| 196 | + onException.run(); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public TransportResponse.Empty read(StreamInput in) { |
| 201 | + return TransportResponse.Empty.INSTANCE; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments