|
9 | 9 |
|
10 | 10 | package org.elasticsearch.repositories.azure; |
11 | 11 |
|
| 12 | +import com.sun.net.httpserver.Headers; |
12 | 13 | import com.sun.net.httpserver.HttpExchange; |
13 | 14 | import com.sun.net.httpserver.HttpHandler; |
14 | 15 |
|
15 | 16 | import org.elasticsearch.cluster.node.DiscoveryNode; |
| 17 | +import org.elasticsearch.common.Randomness; |
16 | 18 | import org.elasticsearch.common.blobstore.BlobContainer; |
17 | 19 | import org.elasticsearch.common.blobstore.BlobPath; |
18 | 20 | import org.elasticsearch.common.blobstore.OperationPurpose; |
| 21 | +import org.elasticsearch.common.bytes.BytesArray; |
19 | 22 | import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
20 | 24 | import org.elasticsearch.core.SuppressForbidden; |
21 | 25 | import org.elasticsearch.plugins.PluginsService; |
22 | 26 | import org.elasticsearch.repositories.RepositoriesMetrics; |
|
31 | 35 | import java.io.IOException; |
32 | 36 | import java.nio.ByteBuffer; |
33 | 37 | import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.ArrayList; |
34 | 39 | import java.util.List; |
35 | 40 | import java.util.Map; |
36 | 41 | import java.util.Queue; |
|
43 | 48 | import java.util.stream.IntStream; |
44 | 49 |
|
45 | 50 | import static org.elasticsearch.repositories.azure.AbstractAzureServerTestCase.randomBlobContent; |
| 51 | +import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose; |
46 | 52 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
47 | 53 | import static org.hamcrest.Matchers.hasSize; |
48 | 54 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
@@ -225,6 +231,91 @@ public void testRequestTimeIsAccurate() throws IOException { |
225 | 231 | assertThat(recordedRequestTime, lessThanOrEqualTo(elapsedTimeMillis)); |
226 | 232 | } |
227 | 233 |
|
| 234 | + public void testBatchDeleteFailure() throws IOException { |
| 235 | + final int deleteBatchSize = randomIntBetween(1, 30); |
| 236 | + final String repositoryName = randomRepositoryName(); |
| 237 | + final String repository = createRepository( |
| 238 | + repositoryName, |
| 239 | + Settings.builder() |
| 240 | + .put(repositorySettings(repositoryName)) |
| 241 | + .put(AzureRepository.Repository.DELETION_BATCH_SIZE_SETTING.getKey(), deleteBatchSize) |
| 242 | + .build(), |
| 243 | + true |
| 244 | + ); |
| 245 | + final String dataNodeName = internalCluster().getNodeNameThat(DiscoveryNode::canContainData); |
| 246 | + final BlobContainer container = getBlobContainer(dataNodeName, repository); |
| 247 | + |
| 248 | + final List<String> blobsToDelete = new ArrayList<>(); |
| 249 | + final int numberOfBatches = randomIntBetween(3, 20); |
| 250 | + final int numberOfBlobs = numberOfBatches * deleteBatchSize; |
| 251 | + final int failedBatches = randomIntBetween(1, numberOfBatches); |
| 252 | + for (int i = 0; i < numberOfBlobs; i++) { |
| 253 | + byte[] bytes = randomBytes(randomInt(100)); |
| 254 | + String blobName = "index-" + randomAlphaOfLength(10); |
| 255 | + container.writeBlob(randomPurpose(), blobName, new BytesArray(bytes), false); |
| 256 | + blobsToDelete.add(blobName); |
| 257 | + } |
| 258 | + Randomness.shuffle(blobsToDelete); |
| 259 | + clearMetrics(dataNodeName); |
| 260 | + |
| 261 | + // Handler will fail one or more of the batch requests |
| 262 | + final RequestHandler failNRequestRequestHandler = createFailNRequestsHandler(failedBatches); |
| 263 | + |
| 264 | + // Exhaust the retries |
| 265 | + IntStream.range(0, (numberOfBatches - failedBatches) + (failedBatches * (MAX_RETRIES + 1))) |
| 266 | + .forEach(i -> requestHandlers.offer(failNRequestRequestHandler)); |
| 267 | + |
| 268 | + logger.info("--> Failing {} of {} batches", failedBatches, numberOfBatches); |
| 269 | + |
| 270 | + final IOException exception = assertThrows( |
| 271 | + IOException.class, |
| 272 | + () -> container.deleteBlobsIgnoringIfNotExists(randomPurpose(), blobsToDelete.iterator()) |
| 273 | + ); |
| 274 | + assertEquals(Math.min(failedBatches, 10), exception.getSuppressed().length); |
| 275 | + assertEquals( |
| 276 | + (numberOfBatches - failedBatches) + (failedBatches * (MAX_RETRIES + 1L)), |
| 277 | + getLongCounterTotal(dataNodeName, RepositoriesMetrics.METRIC_REQUESTS_TOTAL) |
| 278 | + ); |
| 279 | + assertEquals((failedBatches * (MAX_RETRIES + 1L)), getLongCounterTotal(dataNodeName, RepositoriesMetrics.METRIC_EXCEPTIONS_TOTAL)); |
| 280 | + assertEquals(failedBatches * deleteBatchSize, container.listBlobs(randomPurpose()).size()); |
| 281 | + } |
| 282 | + |
| 283 | + private long getLongCounterTotal(String dataNodeName, String metricKey) { |
| 284 | + return getTelemetryPlugin(dataNodeName).getLongCounterMeasurement(metricKey) |
| 285 | + .stream() |
| 286 | + .mapToLong(Measurement::getLong) |
| 287 | + .reduce(0L, Long::sum); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Creates a {@link RequestHandler} that will persistently fail the first <code>numberToFail</code> distinct requests |
| 292 | + * it sees. Any other requests are passed through to the delegate. |
| 293 | + * |
| 294 | + * @param numberToFail The number of requests to fail |
| 295 | + * @return the handler |
| 296 | + */ |
| 297 | + private static RequestHandler createFailNRequestsHandler(int numberToFail) { |
| 298 | + final List<String> requestsToFail = new ArrayList<>(numberToFail); |
| 299 | + return (exchange, delegate) -> { |
| 300 | + final Headers requestHeaders = exchange.getRequestHeaders(); |
| 301 | + final String requestId = requestHeaders.get("X-ms-client-request-id").get(0); |
| 302 | + boolean failRequest = false; |
| 303 | + synchronized (requestsToFail) { |
| 304 | + if (requestsToFail.contains(requestId)) { |
| 305 | + failRequest = true; |
| 306 | + } else if (requestsToFail.size() < numberToFail) { |
| 307 | + requestsToFail.add(requestId); |
| 308 | + failRequest = true; |
| 309 | + } |
| 310 | + } |
| 311 | + if (failRequest) { |
| 312 | + exchange.sendResponseHeaders(500, -1); |
| 313 | + } else { |
| 314 | + delegate.handle(exchange); |
| 315 | + } |
| 316 | + }; |
| 317 | + } |
| 318 | + |
228 | 319 | private void clearMetrics(String discoveryNode) { |
229 | 320 | internalCluster().getInstance(PluginsService.class, discoveryNode) |
230 | 321 | .filterPlugins(TestTelemetryPlugin.class) |
|
0 commit comments