-
Notifications
You must be signed in to change notification settings - Fork 2k
/
S3FileIO.java
423 lines (386 loc) · 15.8 KB
/
S3FileIO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iceberg.aws.s3;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.apache.iceberg.aws.AwsClientFactory;
import org.apache.iceberg.aws.S3FileIOAwsClientFactories;
import org.apache.iceberg.common.DynConstructors;
import org.apache.iceberg.io.BulkDeletionFailureException;
import org.apache.iceberg.io.CredentialSupplier;
import org.apache.iceberg.io.DelegateFileIO;
import org.apache.iceberg.io.FileInfo;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.io.OutputFile;
import org.apache.iceberg.metrics.MetricsContext;
import org.apache.iceberg.relocated.com.google.common.base.Joiner;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Multimaps;
import org.apache.iceberg.relocated.com.google.common.collect.SetMultimap;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.relocated.com.google.common.collect.Streams;
import org.apache.iceberg.util.PropertyUtil;
import org.apache.iceberg.util.SerializableMap;
import org.apache.iceberg.util.SerializableSupplier;
import org.apache.iceberg.util.Tasks;
import org.apache.iceberg.util.ThreadPools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Delete;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse;
import software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest;
import software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
import software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.Tag;
import software.amazon.awssdk.services.s3.model.Tagging;
/**
* FileIO implementation backed by S3.
*
* <p>Locations used must follow the conventions for S3 URIs (e.g. s3://bucket/path...). URIs with
* schemes s3a, s3n, https are also treated as s3 file paths. Using this FileIO with other schemes
* will result in {@link org.apache.iceberg.exceptions.ValidationException}.
*/
public class S3FileIO implements CredentialSupplier, DelegateFileIO {
private static final Logger LOG = LoggerFactory.getLogger(S3FileIO.class);
private static final String DEFAULT_METRICS_IMPL =
"org.apache.iceberg.hadoop.HadoopMetricsContext";
private static volatile ExecutorService executorService;
private String credential = null;
private SerializableSupplier<S3Client> s3;
private S3FileIOProperties s3FileIOProperties;
private SerializableMap<String, String> properties = null;
private transient volatile S3Client client;
private MetricsContext metrics = MetricsContext.nullMetrics();
private final AtomicBoolean isResourceClosed = new AtomicBoolean(false);
private transient StackTraceElement[] createStack;
/**
* No-arg constructor to load the FileIO dynamically.
*
* <p>All fields are initialized by calling {@link S3FileIO#initialize(Map)} later.
*/
public S3FileIO() {}
/**
* Constructor with custom s3 supplier and S3FileIO properties.
*
* <p>Calling {@link S3FileIO#initialize(Map)} will overwrite information set in this constructor.
*
* @param s3 s3 supplier
*/
public S3FileIO(SerializableSupplier<S3Client> s3) {
this(s3, new S3FileIOProperties());
}
/**
* Constructor with custom s3 supplier and S3FileIO properties.
*
* <p>Calling {@link S3FileIO#initialize(Map)} will overwrite information set in this constructor.
*
* @param s3 s3 supplier
* @param s3FileIOProperties S3 FileIO properties
*/
public S3FileIO(SerializableSupplier<S3Client> s3, S3FileIOProperties s3FileIOProperties) {
this.s3 = s3;
this.s3FileIOProperties = s3FileIOProperties;
this.createStack = Thread.currentThread().getStackTrace();
}
@Override
public InputFile newInputFile(String path) {
return S3InputFile.fromLocation(path, client(), s3FileIOProperties, metrics);
}
@Override
public InputFile newInputFile(String path, long length) {
return S3InputFile.fromLocation(path, length, client(), s3FileIOProperties, metrics);
}
@Override
public OutputFile newOutputFile(String path) {
return S3OutputFile.fromLocation(path, client(), s3FileIOProperties, metrics);
}
@Override
public void deleteFile(String path) {
if (s3FileIOProperties.deleteTags() != null && !s3FileIOProperties.deleteTags().isEmpty()) {
try {
tagFileToDelete(path, s3FileIOProperties.deleteTags());
} catch (S3Exception e) {
LOG.warn("Failed to add delete tags: {} to {}", s3FileIOProperties.deleteTags(), path, e);
}
}
if (!s3FileIOProperties.isDeleteEnabled()) {
return;
}
S3URI location = new S3URI(path, s3FileIOProperties.bucketToAccessPointMapping());
DeleteObjectRequest deleteRequest =
DeleteObjectRequest.builder().bucket(location.bucket()).key(location.key()).build();
client().deleteObject(deleteRequest);
}
@Override
public Map<String, String> properties() {
return properties.immutableMap();
}
/**
* Deletes the given paths in a batched manner.
*
* <p>The paths are grouped by bucket, and deletion is triggered when we either reach the
* configured batch size or have a final remainder batch for each bucket.
*
* @param paths paths to delete
*/
@Override
public void deleteFiles(Iterable<String> paths) throws BulkDeletionFailureException {
if (s3FileIOProperties.deleteTags() != null && !s3FileIOProperties.deleteTags().isEmpty()) {
Tasks.foreach(paths)
.noRetry()
.executeWith(executorService())
.suppressFailureWhenFinished()
.onFailure(
(path, exc) ->
LOG.warn(
"Failed to add delete tags: {} to {}",
s3FileIOProperties.deleteTags(),
path,
exc))
.run(path -> tagFileToDelete(path, s3FileIOProperties.deleteTags()));
}
if (s3FileIOProperties.isDeleteEnabled()) {
SetMultimap<String, String> bucketToObjects =
Multimaps.newSetMultimap(Maps.newHashMap(), Sets::newHashSet);
List<Future<List<String>>> deletionTasks = Lists.newArrayList();
for (String path : paths) {
S3URI location = new S3URI(path, s3FileIOProperties.bucketToAccessPointMapping());
String bucket = location.bucket();
String objectKey = location.key();
bucketToObjects.get(bucket).add(objectKey);
if (bucketToObjects.get(bucket).size() == s3FileIOProperties.deleteBatchSize()) {
Set<String> keys = Sets.newHashSet(bucketToObjects.get(bucket));
Future<List<String>> deletionTask =
executorService().submit(() -> deleteBatch(bucket, keys));
deletionTasks.add(deletionTask);
bucketToObjects.removeAll(bucket);
}
}
// Delete the remainder
for (Map.Entry<String, Collection<String>> bucketToObjectsEntry :
bucketToObjects.asMap().entrySet()) {
String bucket = bucketToObjectsEntry.getKey();
Collection<String> keys = bucketToObjectsEntry.getValue();
Future<List<String>> deletionTask =
executorService().submit(() -> deleteBatch(bucket, keys));
deletionTasks.add(deletionTask);
}
int totalFailedDeletions = 0;
for (Future<List<String>> deletionTask : deletionTasks) {
try {
List<String> failedDeletions = deletionTask.get();
failedDeletions.forEach(path -> LOG.warn("Failed to delete object at path {}", path));
totalFailedDeletions += failedDeletions.size();
} catch (ExecutionException e) {
LOG.warn("Caught unexpected exception during batch deletion: ", e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
deletionTasks.stream().filter(task -> !task.isDone()).forEach(task -> task.cancel(true));
throw new RuntimeException("Interrupted when waiting for deletions to complete", e);
}
}
if (totalFailedDeletions > 0) {
throw new BulkDeletionFailureException(totalFailedDeletions);
}
}
}
private void tagFileToDelete(String path, Set<Tag> deleteTags) throws S3Exception {
S3URI location = new S3URI(path, s3FileIOProperties.bucketToAccessPointMapping());
String bucket = location.bucket();
String objectKey = location.key();
GetObjectTaggingRequest getObjectTaggingRequest =
GetObjectTaggingRequest.builder().bucket(bucket).key(objectKey).build();
GetObjectTaggingResponse getObjectTaggingResponse =
client().getObjectTagging(getObjectTaggingRequest);
// Get existing tags, if any and then add the delete tags
Set<Tag> tags = Sets.newHashSet();
if (getObjectTaggingResponse.hasTagSet()) {
tags.addAll(getObjectTaggingResponse.tagSet());
}
tags.addAll(deleteTags);
PutObjectTaggingRequest putObjectTaggingRequest =
PutObjectTaggingRequest.builder()
.bucket(bucket)
.key(objectKey)
.tagging(Tagging.builder().tagSet(tags).build())
.build();
client().putObjectTagging(putObjectTaggingRequest);
}
private List<String> deleteBatch(String bucket, Collection<String> keysToDelete) {
List<ObjectIdentifier> objectIds =
keysToDelete.stream()
.map(key -> ObjectIdentifier.builder().key(key).build())
.collect(Collectors.toList());
DeleteObjectsRequest request =
DeleteObjectsRequest.builder()
.bucket(bucket)
.delete(Delete.builder().objects(objectIds).build())
.build();
List<String> failures = Lists.newArrayList();
try {
DeleteObjectsResponse response = client().deleteObjects(request);
if (response.hasErrors()) {
failures.addAll(
response.errors().stream()
.map(error -> String.format("s3://%s/%s", request.bucket(), error.key()))
.collect(Collectors.toList()));
}
} catch (Exception e) {
LOG.warn("Encountered failure when deleting batch", e);
failures.addAll(
request.delete().objects().stream()
.map(obj -> String.format("s3://%s/%s", request.bucket(), obj.key()))
.collect(Collectors.toList()));
}
return failures;
}
@Override
public Iterable<FileInfo> listPrefix(String prefix) {
S3URI s3uri = new S3URI(prefix, s3FileIOProperties.bucketToAccessPointMapping());
ListObjectsV2Request request =
ListObjectsV2Request.builder().bucket(s3uri.bucket()).prefix(s3uri.key()).build();
return () ->
client().listObjectsV2Paginator(request).stream()
.flatMap(r -> r.contents().stream())
.map(
o ->
new FileInfo(
String.format("%s://%s/%s", s3uri.scheme(), s3uri.bucket(), o.key()),
o.size(),
o.lastModified().toEpochMilli()))
.iterator();
}
/**
* This method provides a "best-effort" to delete all objects under the given prefix.
*
* <p>Bulk delete operations are used and no reattempt is made for deletes if they fail, but will
* log any individual objects that are not deleted as part of the bulk operation.
*
* @param prefix prefix to delete
*/
@Override
public void deletePrefix(String prefix) {
deleteFiles(() -> Streams.stream(listPrefix(prefix)).map(FileInfo::location).iterator());
}
public S3Client client() {
if (client == null) {
synchronized (this) {
if (client == null) {
client = s3.get();
}
}
}
return client;
}
private ExecutorService executorService() {
if (executorService == null) {
synchronized (S3FileIO.class) {
if (executorService == null) {
executorService =
ThreadPools.newWorkerPool(
"iceberg-s3fileio-delete", s3FileIOProperties.deleteThreads());
}
}
}
return executorService;
}
@Override
public String getCredential() {
return credential;
}
@Override
public void initialize(Map<String, String> props) {
this.properties = SerializableMap.copyOf(props);
this.s3FileIOProperties = new S3FileIOProperties(properties);
this.createStack =
PropertyUtil.propertyAsBoolean(props, "init-creation-stacktrace", true)
? Thread.currentThread().getStackTrace()
: null;
// Do not override s3 client if it was provided
if (s3 == null) {
Object clientFactory = S3FileIOAwsClientFactories.initialize(props);
if (clientFactory instanceof S3FileIOAwsClientFactory) {
this.s3 = ((S3FileIOAwsClientFactory) clientFactory)::s3;
}
if (clientFactory instanceof AwsClientFactory) {
this.s3 = ((AwsClientFactory) clientFactory)::s3;
}
if (clientFactory instanceof CredentialSupplier) {
this.credential = ((CredentialSupplier) clientFactory).getCredential();
}
if (s3FileIOProperties.isPreloadClientEnabled()) {
client();
}
}
initMetrics(properties);
}
@SuppressWarnings("CatchBlockLogException")
private void initMetrics(Map<String, String> props) {
// Report Hadoop metrics if Hadoop is available
try {
DynConstructors.Ctor<MetricsContext> ctor =
DynConstructors.builder(MetricsContext.class)
.hiddenImpl(DEFAULT_METRICS_IMPL, String.class)
.buildChecked();
MetricsContext context = ctor.newInstance("s3");
context.initialize(props);
this.metrics = context;
} catch (NoClassDefFoundError | NoSuchMethodException | ClassCastException e) {
LOG.warn(
"Unable to load metrics class: '{}', falling back to null metrics", DEFAULT_METRICS_IMPL);
}
}
@Override
public void close() {
// handles concurrent calls to close()
if (isResourceClosed.compareAndSet(false, true)) {
if (client != null) {
client.close();
}
}
}
@SuppressWarnings("checkstyle:NoFinalizer")
@Override
protected void finalize() throws Throwable {
super.finalize();
if (!isResourceClosed.get()) {
close();
if (null != createStack) {
String trace =
Joiner.on("\n\t").join(Arrays.copyOfRange(createStack, 1, createStack.length));
LOG.warn("Unclosed S3FileIO instance created by:\n\t{}", trace);
}
}
}
}