|
| 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; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.fs.FileSystem; |
| 31 | + |
| 32 | +public class ITestS3AFileSystemIsolatedClassloader extends AbstractS3ATestBase { |
| 33 | + |
| 34 | + public static class CustomClassLoader extends ClassLoader { |
| 35 | + } |
| 36 | + |
| 37 | + private final ClassLoader customClassLoader = new CustomClassLoader(); |
| 38 | + |
| 39 | + private S3AFileSystem createNewTestFs(Configuration conf) { |
| 40 | + try { |
| 41 | + S3AFileSystem fs = new S3AFileSystem(); |
| 42 | + fs.initialize(getFileSystem().getUri(), conf); |
| 43 | + return fs; |
| 44 | + } catch (IOException e) { |
| 45 | + throw new RuntimeException(e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void test(Map<String, String> confToSet, Consumer<FileSystem> asserts) |
| 50 | + throws IOException { |
| 51 | + ClassLoader previousClassloader = Thread.currentThread().getContextClassLoader(); |
| 52 | + try { |
| 53 | + Thread.currentThread().setContextClassLoader(customClassLoader); |
| 54 | + Configuration conf = new Configuration(); |
| 55 | + Assertions.assertThat(conf.getClassLoader()).isEqualTo(customClassLoader); |
| 56 | + S3ATestUtils.prepareTestConfiguration(conf); |
| 57 | + for (Map.Entry<String, String> e : confToSet.entrySet()) { |
| 58 | + conf.set(e.getKey(), e.getValue()); |
| 59 | + } |
| 60 | + try (S3AFileSystem fs = createNewTestFs(conf)) { |
| 61 | + asserts.accept(fs); |
| 62 | + } |
| 63 | + } finally { |
| 64 | + Thread.currentThread().setContextClassLoader(previousClassloader); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private Map<String, String> mapOf() { |
| 69 | + return new HashMap<>(); |
| 70 | + } |
| 71 | + |
| 72 | + private Map<String, String> mapOf(String key, String value) { |
| 73 | + HashMap<String, String> m = new HashMap<>(); |
| 74 | + m.put(key, value); |
| 75 | + return m; |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void defaultIsolatedClassloader() throws IOException { |
| 80 | + test(mapOf(), (fs) -> { |
| 81 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 82 | + .describedAs("The classloader used to load s3a fs extensions") |
| 83 | + .isNotEqualTo(Thread.currentThread().getContextClassLoader()) |
| 84 | + .describedAs("the current classloader"); |
| 85 | + |
| 86 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 87 | + .describedAs("The classloader used to load s3a fs extensions") |
| 88 | + .isEqualTo(fs.getClass().getClassLoader()) |
| 89 | + .describedAs("the classloader that loaded the fs"); |
| 90 | + ; |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void isolatedClassloader() throws IOException { |
| 96 | + test(mapOf(Constants.AWS_S3_CLASSLOADER_ISOLATION, "true"), (fs) -> { |
| 97 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 98 | + .describedAs("The classloader used to load s3a fs extensions") |
| 99 | + .isNotEqualTo(Thread.currentThread().getContextClassLoader()) |
| 100 | + .describedAs("the current context classloader"); |
| 101 | + |
| 102 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 103 | + .describedAs("The classloader used to load s3a fs extensions") |
| 104 | + .isEqualTo(fs.getClass().getClassLoader()) |
| 105 | + .describedAs("the classloader that loaded the fs"); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void notIsolatedClassloader() throws IOException { |
| 111 | + test(mapOf(Constants.AWS_S3_CLASSLOADER_ISOLATION, "false"), (fs) -> { |
| 112 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 113 | + .describedAs("The classloader used to load s3a fs extensions") |
| 114 | + .isEqualTo(Thread.currentThread().getContextClassLoader()) |
| 115 | + .describedAs("the current context classloader"); |
| 116 | + |
| 117 | + Assertions.assertThat(fs.getConf().getClassLoader()) |
| 118 | + .describedAs("The classloader used to load s3a fs extensions") |
| 119 | + .isNotEqualTo(fs.getClass().getClassLoader()) |
| 120 | + .describedAs("the classloader that loaded the fs"); |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
0 commit comments