|
| 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.list; |
| 20 | + |
| 21 | +import static org.apache.hadoop.fs.s3a.Constants.FAST_UPLOAD_BUFFER; |
| 22 | +import static org.apache.hadoop.fs.s3a.Constants.FAST_UPLOAD_BUFFER_ARRAY; |
| 23 | +import static org.apache.hadoop.fs.s3a.Constants.FAST_UPLOAD_BUFFER_DISK; |
| 24 | +import static org.apache.hadoop.fs.s3a.Constants.READ_RESTORED_GLACIER_OBJECTS; |
| 25 | +import static org.apache.hadoop.fs.s3a.Constants.STORAGE_CLASS; |
| 26 | +import static org.apache.hadoop.fs.s3a.Constants.STORAGE_CLASS_GLACIER; |
| 27 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.disableFilesystemCaching; |
| 28 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.removeBaseAndBucketOverrides; |
| 29 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.skipIfStorageClassTestsDisabled; |
| 30 | +import static org.apache.hadoop.fs.s3a.impl.HeaderProcessing.XA_STORAGE_CLASS; |
| 31 | +import static org.apache.hadoop.fs.s3a.impl.HeaderProcessing.decodeBytes; |
| 32 | +import static org.apache.hadoop.test.LambdaTestUtils.intercept; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.nio.file.AccessDeniedException; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.Map; |
| 39 | +import org.apache.hadoop.conf.Configuration; |
| 40 | +import org.apache.hadoop.fs.FileSystem; |
| 41 | +import org.apache.hadoop.fs.Path; |
| 42 | +import org.apache.hadoop.fs.contract.ContractTestUtils; |
| 43 | +import org.apache.hadoop.fs.contract.s3a.S3AContract; |
| 44 | +import org.apache.hadoop.fs.s3a.AbstractS3ATestBase; |
| 45 | +import org.apache.hadoop.fs.s3a.S3AFileSystem; |
| 46 | +import org.apache.hadoop.fs.s3a.S3ObjectStorageClassFilter; |
| 47 | +import org.assertj.core.api.Assertions; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | +import org.junit.runners.Parameterized; |
| 51 | + |
| 52 | +@RunWith(Parameterized.class) |
| 53 | +public class ITestS3AReadRestoredGlacierObjects extends AbstractS3ATestBase { |
| 54 | + |
| 55 | + @Parameterized.Parameters(name = "fast-upload-buffer-{0}") |
| 56 | + public static Collection<Object[]> params() { |
| 57 | + return Arrays.asList(new Object[][]{ |
| 58 | + {FAST_UPLOAD_BUFFER_DISK}, |
| 59 | + {FAST_UPLOAD_BUFFER_ARRAY} |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private final String fastUploadBufferType; |
| 64 | + |
| 65 | + public ITestS3AReadRestoredGlacierObjects(String fastUploadBufferType) { |
| 66 | + this.fastUploadBufferType = fastUploadBufferType; |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testIgnoreGlacierObject() throws Throwable { |
| 71 | + try (FileSystem fs = createFiles(S3ObjectStorageClassFilter.SKIP_ALL_GLACIER.name())) { |
| 72 | + Assertions.assertThat( |
| 73 | + fs.listStatus(methodPath())) |
| 74 | + .describedAs("FileStatus List of %s", methodPath()).isEmpty(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testIgnoreRestoringGlacierObject() throws Throwable { |
| 80 | + try (FileSystem fs = createFiles(S3ObjectStorageClassFilter.READ_RESTORED_GLACIER_OBJECTS.name())) { |
| 81 | + Assertions.assertThat( |
| 82 | + fs.listStatus( |
| 83 | + methodPath())) |
| 84 | + .describedAs("FileStatus List of %s", methodPath()).isEmpty(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testDefault() throws Throwable { |
| 90 | + try (FileSystem fs = createFiles(S3ObjectStorageClassFilter.READ_ALL.name())) { |
| 91 | + Assertions.assertThat( |
| 92 | + fs.listStatus(methodPath())) |
| 93 | + .describedAs("FileStatus List of %s", methodPath()).isNotEmpty(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected Configuration createConfiguration() { |
| 99 | + Configuration newConf = super.createConfiguration(); |
| 100 | + newConf.set(STORAGE_CLASS, STORAGE_CLASS_GLACIER); // Create Glacier objects |
| 101 | + skipIfStorageClassTestsDisabled(newConf); |
| 102 | + disableFilesystemCaching(newConf); |
| 103 | + removeBaseAndBucketOverrides(newConf, STORAGE_CLASS, FAST_UPLOAD_BUFFER); |
| 104 | + newConf.set(FAST_UPLOAD_BUFFER, fastUploadBufferType); |
| 105 | + return newConf; |
| 106 | + } |
| 107 | + |
| 108 | + private FileSystem createFiles(String s3ObjectStorageClassFilter) throws Throwable { |
| 109 | + Configuration conf = this.createConfiguration(); |
| 110 | + conf.set(STORAGE_CLASS, STORAGE_CLASS_GLACIER); |
| 111 | + conf.set(READ_RESTORED_GLACIER_OBJECTS, s3ObjectStorageClassFilter); |
| 112 | + S3AContract contract = (S3AContract) createContract(conf); |
| 113 | + contract.init(); |
| 114 | + |
| 115 | + FileSystem fs = contract.getTestFileSystem(); |
| 116 | + Path dir = methodPath(); |
| 117 | + fs.mkdirs(dir); |
| 118 | + // even with storage class specified |
| 119 | + // directories do not have storage class |
| 120 | + assertObjectHasNoStorageClass(dir); |
| 121 | + Path path = new Path(dir, "file1"); |
| 122 | + ContractTestUtils.touch(fs, path); |
| 123 | + assertObjectHasStorageClass(path, STORAGE_CLASS_GLACIER); |
| 124 | + return fs; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Assert that a given object has no storage class specified. |
| 129 | + * |
| 130 | + * @param path path |
| 131 | + */ |
| 132 | + protected void assertObjectHasNoStorageClass(Path path) throws Throwable { |
| 133 | + S3AFileSystem fs = getFileSystem(); |
| 134 | + Map<String, byte[]> xAttrs = fs.getXAttrs(path); |
| 135 | + String storageClass = decodeBytes(xAttrs.get(XA_STORAGE_CLASS)); |
| 136 | + |
| 137 | + Assertions.assertThat(storageClass).describedAs("Storage class of object %s", path).isNull(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Assert that a given object has the given storage class specified. |
| 142 | + * |
| 143 | + * @param path path |
| 144 | + * @param expectedStorageClass expected storage class for the object |
| 145 | + */ |
| 146 | + protected void assertObjectHasStorageClass(Path path, String expectedStorageClass) |
| 147 | + throws Throwable { |
| 148 | + S3AFileSystem fs = getFileSystem(); |
| 149 | + Map<String, byte[]> xAttrs = fs.getXAttrs(path); |
| 150 | + String actualStorageClass = decodeBytes(xAttrs.get(XA_STORAGE_CLASS)); |
| 151 | + |
| 152 | + Assertions.assertThat(actualStorageClass).describedAs("Storage class of object %s", path) |
| 153 | + .isEqualToIgnoringCase(expectedStorageClass); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments