|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.s3a.performance; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicLong; |
| 26 | + |
| 27 | +import org.assertj.core.api.Assertions; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 32 | +import software.amazon.awssdk.http.auth.spi.signer.AsyncSignRequest; |
| 33 | +import software.amazon.awssdk.http.auth.spi.signer.AsyncSignedRequest; |
| 34 | +import software.amazon.awssdk.http.auth.spi.signer.HttpSigner; |
| 35 | +import software.amazon.awssdk.http.auth.spi.signer.SignRequest; |
| 36 | +import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; |
| 37 | +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 38 | + |
| 39 | +import org.apache.hadoop.conf.Configuration; |
| 40 | +import org.apache.hadoop.fs.Path; |
| 41 | +import org.apache.hadoop.fs.s3a.AWSApiCallTimeoutException; |
| 42 | +import org.apache.hadoop.fs.s3a.S3AFileSystem; |
| 43 | +import org.apache.hadoop.fs.s3a.auth.CustomHttpSigner; |
| 44 | +import org.apache.hadoop.fs.s3a.impl.AWSClientConfig; |
| 45 | +import org.apache.hadoop.util.DurationInfo; |
| 46 | + |
| 47 | +import static org.apache.hadoop.fs.s3a.Constants.CUSTOM_SIGNERS; |
| 48 | +import static org.apache.hadoop.fs.s3a.Constants.HTTP_SIGNER_CLASS_NAME; |
| 49 | +import static org.apache.hadoop.fs.s3a.Constants.HTTP_SIGNER_ENABLED; |
| 50 | +import static org.apache.hadoop.fs.s3a.Constants.REQUEST_TIMEOUT; |
| 51 | +import static org.apache.hadoop.fs.s3a.Constants.RETRY_LIMIT; |
| 52 | +import static org.apache.hadoop.fs.s3a.Constants.S3A_BUCKET_PROBE; |
| 53 | +import static org.apache.hadoop.fs.s3a.Constants.S3EXPRESS_CREATE_SESSION; |
| 54 | +import static org.apache.hadoop.fs.s3a.Constants.SIGNING_ALGORITHM_S3; |
| 55 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.disableFilesystemCaching; |
| 56 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.removeBaseAndBucketOverrides; |
| 57 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.skipIfNotS3ExpressBucket; |
| 58 | +import static org.apache.hadoop.test.LambdaTestUtils.intercept; |
| 59 | + |
| 60 | +/** |
| 61 | + * Test timeout of S3 Client CreateSession call, which was originally |
| 62 | + * hard coded to 10 seconds. |
| 63 | + * Only executed against an S3Express store. |
| 64 | + */ |
| 65 | +public class ITestCreateSessionTimeout extends AbstractS3ACostTest { |
| 66 | + |
| 67 | + private static final Logger LOG = |
| 68 | + LoggerFactory.getLogger(ITestCreateSessionTimeout.class); |
| 69 | + |
| 70 | + /** |
| 71 | + * What is the duration for the operation after which the test is considered |
| 72 | + * to have failed because timeouts didn't get passed down? |
| 73 | + */ |
| 74 | + private static final long TIMEOUT_EXCEPTION_THRESHOLD = Duration.ofSeconds(5).toMillis(); |
| 75 | + |
| 76 | + /** |
| 77 | + * How long to sleep in requests? |
| 78 | + */ |
| 79 | + private static final AtomicLong SLEEP_DURATION = new AtomicLong( |
| 80 | + Duration.ofSeconds(20).toMillis()); |
| 81 | + |
| 82 | + /** |
| 83 | + * Flag set if the sleep was interrupted during signing. |
| 84 | + */ |
| 85 | + private static final AtomicBoolean SLEEP_INTERRUPTED = new AtomicBoolean(false); |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a configuration with a 10 millisecond timeout on API calls |
| 89 | + * and a custom signer which sleeps much longer than that. |
| 90 | + * @return the configuration. |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public Configuration createConfiguration() { |
| 94 | + final Configuration conf = super.createConfiguration(); |
| 95 | + skipIfNotS3ExpressBucket(conf); |
| 96 | + disableFilesystemCaching(conf); |
| 97 | + removeBaseAndBucketOverrides(conf, |
| 98 | + CUSTOM_SIGNERS, |
| 99 | + HTTP_SIGNER_ENABLED, |
| 100 | + REQUEST_TIMEOUT, |
| 101 | + RETRY_LIMIT, |
| 102 | + S3A_BUCKET_PROBE, |
| 103 | + S3EXPRESS_CREATE_SESSION, |
| 104 | + SIGNING_ALGORITHM_S3 |
| 105 | + ); |
| 106 | + |
| 107 | + conf.setBoolean(HTTP_SIGNER_ENABLED, true); |
| 108 | + conf.setClass(HTTP_SIGNER_CLASS_NAME, SlowSigner.class, HttpSigner.class); |
| 109 | + Duration duration = Duration.ofMillis(10); |
| 110 | + |
| 111 | + conf.setLong(REQUEST_TIMEOUT, duration.toMillis()); |
| 112 | + conf.setInt(RETRY_LIMIT, 1); |
| 113 | + |
| 114 | + return conf; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void setup() throws Exception { |
| 119 | + // remove the safety check on minimum durations. |
| 120 | + AWSClientConfig.setMinimumOperationDuration(Duration.ZERO); |
| 121 | + try { |
| 122 | + super.setup(); |
| 123 | + } finally { |
| 124 | + // restore the safety check on minimum durations. |
| 125 | + AWSClientConfig.resetMinimumOperationDuration(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected void deleteTestDirInTeardown() { |
| 131 | + // no-op |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Make this a no-op to avoid IO. |
| 136 | + * @param path path path |
| 137 | + */ |
| 138 | + @Override |
| 139 | + protected void mkdirs(Path path) { |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testSlowSigningTriggersTimeout() throws Throwable { |
| 145 | + |
| 146 | + final S3AFileSystem fs = getFileSystem(); |
| 147 | + DurationInfo call = new DurationInfo(LOG, true, "Create session"); |
| 148 | + final AWSApiCallTimeoutException thrown = intercept(AWSApiCallTimeoutException.class, |
| 149 | + () -> fs.getFileStatus(path("testShortTimeout"))); |
| 150 | + call.finished(); |
| 151 | + LOG.info("Exception raised after {}", call, thrown); |
| 152 | + // if the timeout took too long, fail with details and include the original |
| 153 | + // exception |
| 154 | + if (call.value() > TIMEOUT_EXCEPTION_THRESHOLD) { |
| 155 | + throw new AssertionError("Duration of create session " + call.getDurationString() |
| 156 | + + " exceeds threshold " + TIMEOUT_EXCEPTION_THRESHOLD + " ms: " + thrown, thrown); |
| 157 | + } |
| 158 | + Assertions.assertThat(SLEEP_INTERRUPTED.get()) |
| 159 | + .describedAs("Sleep interrupted during signing") |
| 160 | + .isTrue(); |
| 161 | + |
| 162 | + // now scan the inner exception stack for "createSession" |
| 163 | + Arrays.stream(thrown.getCause().getStackTrace()) |
| 164 | + .filter(e -> e.getMethodName().equals("createSession")) |
| 165 | + .findFirst() |
| 166 | + .orElseThrow(() -> |
| 167 | + new AssertionError("No createSession() in inner stack trace of", thrown)); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Sleep for as long as {@link #SLEEP_DURATION} requires. |
| 172 | + */ |
| 173 | + private static void sleep() { |
| 174 | + long sleep = SLEEP_DURATION.get(); |
| 175 | + if (sleep > 0) { |
| 176 | + LOG.info("Sleeping for {} ms", sleep, new Exception()); |
| 177 | + try (DurationInfo d = new DurationInfo(LOG, true, "Sleep for %d ms", sleep)) { |
| 178 | + Thread.sleep(sleep); |
| 179 | + } catch (InterruptedException e) { |
| 180 | + LOG.info("Interrupted", e); |
| 181 | + SLEEP_INTERRUPTED.set(true); |
| 182 | + Thread.currentThread().interrupt(); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * A signer which calls {@link #sleep()} before signing. |
| 189 | + * As this signing takes place within the CreateSession Pipeline, |
| 190 | + */ |
| 191 | + public static class SlowSigner extends CustomHttpSigner { |
| 192 | + |
| 193 | + @Override |
| 194 | + public SignedRequest sign( |
| 195 | + final SignRequest<? extends AwsCredentialsIdentity> request) { |
| 196 | + |
| 197 | + final SdkHttpRequest httpRequest = request.request(); |
| 198 | + LOG.info("Signing request {}", httpRequest); |
| 199 | + sleep(); |
| 200 | + return super.sign(request); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public CompletableFuture<AsyncSignedRequest> signAsync( |
| 205 | + final AsyncSignRequest<? extends AwsCredentialsIdentity> request) { |
| 206 | + sleep(); |
| 207 | + return super.signAsync(request); |
| 208 | + } |
| 209 | + |
| 210 | + } |
| 211 | +} |
0 commit comments