FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

storage: delete blobs faster (#2972) · googleapis/google-cloud-java@6ca73d3 · GitHub

Commit 6ca73d3

Browse files
authored
storage: delete blobs faster (#2972)
Fixes #2962. The stack trace from the linked issue suggests we are deleting blobs too slowly, leading to timeouts. This PR tackles the problem in two ways. 1. Unless a test is actually testing blob deletion, the test is no longer responsible for cleaning up after itself. Blobs left over are batch-deleted at the end. 2. In case the batch-deletion fails, we set the bucket to auto-delete blobs over 1 day old. Then the clean up job will delete buckets over 2 days old. These buckets should already be empty and easy to delete.
1 parent 54cd111 commit 6ca73d3

3 files changed

Lines changed: 75 additions & 66 deletions

File tree

‎google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616

1717
package com.google.cloud.storage.testing;
1818

19+
import com.google.api.gax.paging.Page;
20+
import com.google.api.gax.retrying.RetrySettings;
1921
import com.google.auth.oauth2.GoogleCredentials;
2022
import com.google.cloud.http.HttpTransportOptions;
21-
import com.google.api.gax.retrying.RetrySettings;
23+
import com.google.cloud.storage.BlobId;
2224
import com.google.cloud.storage.BlobInfo;
25+
import com.google.cloud.storage.Bucket;
2326
import com.google.cloud.storage.Storage;
2427
import com.google.cloud.storage.Storage.BlobListOption;
2528
import com.google.cloud.storage.StorageException;
2629
import com.google.cloud.storage.StorageOptions;
27-
2830
import java.io.IOException;
2931
import java.io.InputStream;
32+
import java.util.ArrayList;
3033
import java.util.UUID;
3134
import java.util.concurrent.Callable;
3235
import java.util.concurrent.ExecutionException;
@@ -67,6 +70,33 @@ public StorageOptions getOptions() {
6770
return options;
6871
}
6972

73+
public static void cleanBuckets(final Storage storage, final long olderThan, long timeoutMs) {
74+
Runnable task =
75+
new Runnable() {
76+
@Override
77+
public void run() {
78+
Page<Bucket> buckets =
79+
storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
80+
for (Bucket bucket : buckets.iterateAll()) {
81+
if (bucket.getCreateTime() < olderThan) {
82+
try {
83+
forceDelete(storage, bucket.getName());
84+
} catch (Exception e) {
85+
// Ignore the exception, maybe the bucket is being deleted by someone else.
86+
}
87+
}
88+
}
89+
}
90+
};
91+
Thread thread = new Thread(task);
92+
thread.start();
93+
try {
94+
thread.join(timeoutMs);
95+
} catch (InterruptedException e) {
96+
log.info("cleanBuckets interrupted");
97+
}
98+
}
99+
70100
/**
71101
* Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket
72102
* deletion succeeds or {@code timeout} expires. To allow for the timeout, this method uses a
@@ -184,8 +214,12 @@ public DeleteBucketTask(Storage storage, String bucket) {
184214
@Override
185215
public Boolean call() {
186216
while (true) {
217+
ArrayList<BlobId> ids = new ArrayList<>();
187218
for (BlobInfo info : storage.list(bucket, BlobListOption.versions(true)).getValues()) {
188-
storage.delete(info.getBlobId());
219+
ids.add(info.getBlobId());
220+
}
221+
if (!ids.isEmpty()) {
222+
storage.delete(ids);
189223
}
190224
try {
191225
storage.delete(bucket);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL