|
1 | 1 | package org.embulk.output; |
2 | 2 |
|
3 | 3 | import com.google.api.services.storage.Storage; |
| 4 | +import com.google.api.services.storage.model.StorageObject; |
| 5 | +import com.google.api.services.storage.model.Objects; |
4 | 6 | import com.google.common.base.Throwables; |
5 | 7 | import org.embulk.config.ConfigDiff; |
6 | 8 | import org.embulk.config.ConfigException; |
|
11 | 13 | import org.embulk.spi.FileOutputPlugin; |
12 | 14 | import org.embulk.spi.TransactionalFileOutput; |
13 | 15 | import org.embulk.spi.unit.LocalFile; |
| 16 | +import org.embulk.spi.util.RetryExecutor.RetryGiveupException; |
| 17 | +import org.embulk.spi.util.RetryExecutor.Retryable; |
| 18 | +import org.slf4j.Logger; |
14 | 19 |
|
15 | 20 | import java.io.IOException; |
| 21 | +import java.io.InterruptedIOException; |
16 | 22 | import java.security.GeneralSecurityException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.LinkedList; |
17 | 25 | import java.util.List; |
18 | 26 | import java.util.Optional; |
19 | 27 | import java.util.function.Function; |
20 | 28 |
|
| 29 | +import static org.embulk.spi.util.RetryExecutor.retryExecutor; |
| 30 | + |
21 | 31 | public class GcsOutputPlugin implements FileOutputPlugin |
22 | 32 | { |
| 33 | + private static final Logger logger = Exec.getLogger(GcsOutputPlugin.class); |
| 34 | + |
23 | 35 | @Override |
24 | 36 | public ConfigDiff transaction(ConfigSource config, |
25 | 37 | int taskCount, |
@@ -50,6 +62,10 @@ else if (task.getAuthMethod().getString().equals("private_key")) { |
50 | 62 | } |
51 | 63 | } |
52 | 64 |
|
| 65 | + if (task.getDeleteInAdvance()) { |
| 66 | + deleteFiles(task); |
| 67 | + } |
| 68 | + |
53 | 69 | return resume(task.dump(), taskCount, control); |
54 | 70 | } |
55 | 71 |
|
@@ -78,6 +94,133 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) |
78 | 94 | return new GcsTransactionalFileOutput(task, client, taskIndex); |
79 | 95 | } |
80 | 96 |
|
| 97 | + private void deleteFiles(PluginTask task) |
| 98 | + { |
| 99 | + logger.info("Start delete files operation"); |
| 100 | + Storage client = createClient(task); |
| 101 | + try { |
| 102 | + List<StorageObject> items = listObjectsWithRetry(client, task.getBucket(), task.getPathPrefix(), task.getMaxConnectionRetry()); |
| 103 | + if (items.size() == 0) { |
| 104 | + logger.info("no files were found"); |
| 105 | + return; |
| 106 | + } |
| 107 | + for (StorageObject item : items) { |
| 108 | + deleteObjectWithRetry(client, item, task.getMaxConnectionRetry()); |
| 109 | + logger.info("delete file: {}/{}", item.getBucket(), item.getName()); |
| 110 | + } |
| 111 | + } |
| 112 | + catch (IOException ex) { |
| 113 | + throw new ConfigException(ex); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private List<StorageObject> listObjectsWithRetry(Storage client, String bucket, String prefix, int maxConnectionRetry) throws IOException |
| 118 | + { |
| 119 | + try { |
| 120 | + return retryExecutor() |
| 121 | + .withRetryLimit(maxConnectionRetry) |
| 122 | + .withInitialRetryWait(500) |
| 123 | + .withMaxRetryWait(30 * 1000) |
| 124 | + .runInterruptible(new Retryable<List<StorageObject>>() { |
| 125 | + @Override |
| 126 | + public List<StorageObject> call() throws IOException |
| 127 | + { |
| 128 | + Storage.Objects.List listObjects = client.objects().list(bucket).setDelimiter("/").setPrefix(prefix); |
| 129 | + List<StorageObject> items = new LinkedList<StorageObject>(); |
| 130 | + String token = null; |
| 131 | + do { |
| 132 | + Objects objects = listObjects.execute(); |
| 133 | + if (objects.getItems() == null) { |
| 134 | + break; |
| 135 | + } |
| 136 | + items.addAll(objects.getItems()); |
| 137 | + token = objects.getNextPageToken(); |
| 138 | + listObjects.setPageToken(token); |
| 139 | + } while (token != null); |
| 140 | + return items; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean isRetryableException(Exception exception) |
| 145 | + { |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait) throws RetryGiveupException |
| 151 | + { |
| 152 | + String message = String.format("GCS list request failed. Retrying %d/%d after %d seconds. Message: %s: %s", |
| 153 | + retryCount, retryLimit, retryWait / 1000, exception.getClass(), exception.getMessage()); |
| 154 | + if (retryCount % 3 == 0) { |
| 155 | + logger.warn(message, exception); |
| 156 | + } |
| 157 | + else { |
| 158 | + logger.warn(message); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onGiveup(Exception firstException, Exception lastException) throws RetryGiveupException |
| 164 | + { |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + catch (RetryGiveupException ex) { |
| 169 | + throw Throwables.propagate(ex.getCause()); |
| 170 | + } |
| 171 | + catch (InterruptedException ex) { |
| 172 | + throw new InterruptedIOException(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private Void deleteObjectWithRetry(Storage client, StorageObject item, int maxConnectionRetry) throws IOException |
| 177 | + { |
| 178 | + try { |
| 179 | + return retryExecutor() |
| 180 | + .withRetryLimit(maxConnectionRetry) |
| 181 | + .withInitialRetryWait(500) |
| 182 | + .withMaxRetryWait(30 * 1000) |
| 183 | + .runInterruptible(new Retryable<Void>() { |
| 184 | + @Override |
| 185 | + public Void call() throws IOException |
| 186 | + { |
| 187 | + client.objects().delete(item.getBucket(), item.getName()).execute(); |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public boolean isRetryableException(Exception exception) |
| 193 | + { |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait) throws RetryGiveupException |
| 199 | + { |
| 200 | + String message = String.format("GCS delete request failed. Retrying %d/%d after %d seconds. Message: %s: %s", |
| 201 | + retryCount, retryLimit, retryWait / 1000, exception.getClass(), exception.getMessage()); |
| 202 | + if (retryCount % 3 == 0) { |
| 203 | + logger.warn(message, exception); |
| 204 | + } |
| 205 | + else { |
| 206 | + logger.warn(message); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public void onGiveup(Exception firstException, Exception lastException) throws RetryGiveupException |
| 212 | + { |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | + catch (RetryGiveupException ex) { |
| 217 | + throw Throwables.propagate(ex.getCause()); |
| 218 | + } |
| 219 | + catch (InterruptedException ex) { |
| 220 | + throw new InterruptedIOException(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
81 | 224 | private GcsAuthentication newGcsAuth(PluginTask task) |
82 | 225 | { |
83 | 226 | try { |
|
0 commit comments