-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCloudSyncVerticle.java
More file actions
393 lines (340 loc) · 14.7 KB
/
CloudSyncVerticle.java
File metadata and controls
393 lines (340 loc) · 14.7 KB
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
package com.uid2.shared.vertx;
import com.uid2.shared.Const;
import com.uid2.shared.cloud.CloudStorageException;
import com.uid2.shared.cloud.ICloudStorage;
import com.uid2.shared.health.HealthComponent;
import com.uid2.shared.health.HealthManager;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Metrics;
import io.vertx.core.*;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
//
// consumes events:
// - cloudsync.<name>.refresh
//
// consumes events:
// - cloudsync.<name>.upload
//
// produces events:
// - cloudsync.<name>.downloaded (String path, from s3-refresh)
// - cloudsync.<name>.refreshed (from s3-refresh)
//
public class CloudSyncVerticle extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudSyncVerticle.class);
private final HealthComponent healthComponent;
private final Counter counterRefreshed;
private final Counter counterRefreshSkipped;
private final Counter counterDownloaded;
private final Counter counterUploaded;
private final Counter counterDownloadFailures;
private final Counter counterUploadFailures;
private final Gauge gaugeConsecutiveRefreshFailures;
private final String name;
private final ICloudStorage cloudStorage;
private final ICloudStorage localStorage;
private final ICloudSync cloudSync;
private final int downloadThreads;
private final int uploadThreads;
private final AtomicInteger storeRefreshIsFailing = new AtomicInteger(0);
private final String eventRefresh;
private final String eventRefreshed;
private final String eventUpload;
private final String eventDownloaded;
private final HashSet<String> pendingUpload = new HashSet<>();
private final HashSet<String> pendingDownload = new HashSet<>();
private WorkerExecutor downloadExecutor = null;
private WorkerExecutor uploadExecutor = null;
private boolean isRefreshing = false;
public CloudSyncVerticle(String name, ICloudStorage cloudStorage, ICloudStorage localStorage,
ICloudSync cloudSync, JsonObject jsonConfig) {
this(name, cloudStorage, localStorage, cloudSync,
jsonConfig.getInteger(Const.Config.CloudDownloadThreadsProp),
jsonConfig.getInteger(Const.Config.CloudUploadThreadsProp));
}
public CloudSyncVerticle(String name, ICloudStorage cloudStorage, ICloudStorage localStorage,
ICloudSync cloudSync, int downloadThreads, int uploadThreads) {
this.healthComponent = HealthManager.instance.registerComponent("cloudsync-" + name);
this.healthComponent.setHealthStatus(false, "not started");
this.name = name;
this.cloudStorage = cloudStorage;
this.localStorage = localStorage;
this.cloudSync = cloudSync;
this.downloadThreads = downloadThreads;
this.uploadThreads = uploadThreads;
String eventPrefix = "cloudsync." + this.name + ".";
this.eventUpload = eventPrefix + "upload";
this.eventDownloaded = eventPrefix + "downloaded";
this.eventRefresh = eventPrefix + "refresh";
this.eventRefreshed = eventPrefix + "refreshed";
Gauge.builder("uid2.cloud_downloading", () -> this.pendingDownload.size())
.tag("store", name)
.description("gauge for how many s3 files are pending download")
.register(Metrics.globalRegistry);
Gauge.builder("uid2.cloud_uploading", () -> this.pendingUpload.size())
.tag("store", name)
.description("gauge for how many s3 files are pending upload")
.register(Metrics.globalRegistry);
this.counterRefreshed = Counter
.builder("uid2.cloud_refreshed")
.tag("store", name)
.description("counter for how many times cloud storage files are refreshed")
.register(Metrics.globalRegistry);
this.counterRefreshSkipped = Counter
.builder("uid2.cloud_refresh_skipped")
.tag("store", name)
.description("counter for how many times cloud storage refresh events are skipped due to in-progress refreshing")
.register(Metrics.globalRegistry);
this.counterDownloaded = Counter
.builder("uid2.cloud_downloaded")
.tag("store", name)
.description("counter for how many cloud files are downloaded")
.register(Metrics.globalRegistry);
this.counterUploaded = Counter
.builder("uid2.cloud_uploaded")
.tag("store", name)
.description("counter for how many cloud files are uploaded")
.register(Metrics.globalRegistry);
this.counterDownloadFailures = Counter
.builder("uid2.cloud_download_failures")
.tag("store", name)
.description("counter for how many cloud files downloads have failed")
.register(Metrics.globalRegistry);
this.counterUploadFailures = Counter
.builder("uid2.cloud_upload_failures")
.tag("store", name)
.description("counter for how many cloud files uploads have failed")
.register(Metrics.globalRegistry);
this.gaugeConsecutiveRefreshFailures = Gauge
.builder("uid2.cloud_downloaded.consecutive_refresh_failures", () -> this.storeRefreshIsFailing.get())
.tag("store", name)
.description("gauge for number of consecutive " + name + " store refresh failures")
.register(Metrics.globalRegistry);
}
@Override
public void start(Promise<Void> promise) {
LOGGER.info("starting CloudSyncVerticle." + name);
this.healthComponent.setHealthStatus(false, "still starting");
this.downloadExecutor = vertx.createSharedWorkerExecutor("cloudsync-" + name + "-download-pool",
this.downloadThreads);
this.uploadExecutor = vertx.createSharedWorkerExecutor("cloudsync-" + name + "-upload-pool",
this.uploadThreads);
// handle refresh event
vertx.eventBus().consumer(
eventRefresh,
o -> this.handleRefresh(o));
// upload to cloud
vertx.eventBus().<String>consumer(
this.eventUpload,
msg -> this.handleUpload(msg));
cloudRefresh()
.onFailure(t -> LOGGER.error("cloudRefresh failed: " + t.getMessage(), new Exception(t)))
.onComplete(ar -> promise.handle(ar));
promise.future()
.onSuccess(v -> {
LOGGER.info("started CloudSyncVerticle." + name);
this.healthComponent.setHealthStatus(true);
})
.onFailure(e -> {
LOGGER.error("failed starting CloudSyncVerticle." + name, new Exception(e));
this.healthComponent.setHealthStatus(false, e.getMessage());
});
}
@Override
public void stop() {
LOGGER.info("shutting down CloudSyncVerticle" + name);
}
public String eventRefresh() {
return eventRefresh;
}
public String eventRefreshed() {
return eventRefreshed;
}
public String eventUpload() {
return eventUpload;
}
public String eventDownloaded() {
return eventDownloaded;
}
private void handleRefresh(Message m) {
cloudRefresh()
.onSuccess(t -> this.storeRefreshIsFailing.set(0))
.onFailure(t -> {
this.storeRefreshIsFailing.set(1);
LOGGER.error("handleRefresh error: " + t.getMessage(), new Exception(t));
});
}
private Future<Void> cloudRefresh() {
if (this.isRefreshing) {
LOGGER.debug("existing s3 refresh in-progress, skipping this one");
counterRefreshSkipped.increment();
return Future.succeededFuture();
}
Promise<Void> refreshPromise = Promise.promise();
this.isRefreshing = true;
vertx.<Void>executeBlocking(blockBromise -> {
this.cloudRefreshEnsureInSync(refreshPromise, 0);
blockBromise.complete();
}, ar -> {});
return refreshPromise.future()
.onComplete(v -> {
this.isRefreshing = false;
emitRefreshedEvent();
});
}
// this is a blocking function
private void cloudRefreshEnsureInSync(Promise<Void> refreshPromise, int iteration) {
try {
final List<Future> fs = new ArrayList<>();
final boolean inSync = this.cloudSync.refresh(
Instant.now(),
this.cloudStorage,
this.localStorage,
downloads -> {
for (String d : downloads) {
fs.add(this.cloudDownloadFile(d));
}
},
deletes -> {
for (String d : deletes) {
fs.add(this.localDelete(d));
}
});
CompositeFuture.all(fs)
.onFailure(e -> refreshPromise.fail(new Exception(e)))
.onSuccess(v -> {
if (inSync) {
refreshPromise.complete();
}
else if (iteration >= 19) {
refreshPromise.fail(new Exception("Cannot full sync in 20 refresh iterations"));
} else {
if (iteration > 1) {
LOGGER.warn("Not synced in " + (iteration + 1) + " iterations, " + "" +
"last iteration contains " + fs.size() + " download/delete jobs");
}
cloudRefreshEnsureInSync(refreshPromise, iteration + 1);
}
});
} catch (CloudStorageException e) {
refreshPromise.fail(new Exception(e));
} catch (Exception e) {
refreshPromise.fail(new Exception("unexpected error in cloudRefresh(): " + e.getMessage(), e));
}
}
private Future<Void> localDelete(String fileToDelete) {
boolean success;
try {
localStorage.delete(fileToDelete);
success = true;
} catch (Exception ex) {
success = false;
}
LOGGER.info("delete " + fileToDelete + ": " + success);
return Future.succeededFuture();
}
private void emitRefreshedEvent() {
int iterations = (int) this.counterRefreshed.count();
this.counterRefreshed.increment();
LOGGER.trace("cloudsync " + this.name + " refreshed " + iterations);
vertx.eventBus().publish(this.eventRefreshed, iterations);
}
private void handleUpload(Message<String> msg) {
String fileToUpload = msg.body();
if (fileToUpload == null) {
LOGGER.warn("Received null filename for s3 upload, likely snapshot/log produce failed");
msg.reply(false);
return;
} else if (this.pendingUpload.contains(fileToUpload)) {
LOGGER.warn("Skip due to upload pending: " + fileToUpload);
msg.reply(false);
return;
} else {
LOGGER.info("Uploading: " + fileToUpload);
this.pendingUpload.add(fileToUpload);
}
this.uploadExecutor.<Void>executeBlocking(
promise -> this.cloudUploadBlocking(promise, msg.body()),
ar -> {
this.pendingUpload.remove(fileToUpload);
this.handleAsyncResult(ar);
msg.reply(ar.succeeded());
// increase counter
if (ar.succeeded()) {
this.counterUploaded.increment();
} else {
this.counterUploadFailures.increment();
}
LOGGER.info("Upload result: " + ar.succeeded() + ", " + fileToUpload);
});
}
private void cloudUploadBlocking(Promise<Void> promise, String fileToUpload) {
try {
String cloudPath = this.cloudSync.toCloudPath(fileToUpload);
try (InputStream localInput = this.localStorage.download(fileToUpload)) {
this.cloudStorage.upload(localInput, cloudPath);
}
promise.complete();
} catch (Exception ex) {
LOGGER.error(ex.getMessage(), ex);
promise.fail(new Throwable(ex));
}
}
private Future<Void> cloudDownloadFile(String s3Path) {
if (s3Path == null) {
LOGGER.warn("Received null path for s3 download");
return Future.succeededFuture();
} else if (this.pendingDownload.contains(s3Path)) {
LOGGER.warn("Skip due to download pending: " + cloudStorage.mask(s3Path));
return Future.succeededFuture();
} else {
LOGGER.trace("Downloading: " + cloudStorage.mask(s3Path));
this.pendingDownload.add(s3Path);
}
Promise<Void> promise = Promise.promise();
this.downloadExecutor.<Void>executeBlocking(
blockingPromise -> this.cloudDownloadBlocking(blockingPromise, s3Path),
ar -> {
this.pendingDownload.remove(s3Path);
this.handleAsyncResult(ar);
promise.complete();
// increase counter, send event
if (ar.succeeded()) {
vertx.eventBus().publish(this.eventDownloaded, this.cloudSync.toLocalPath(s3Path));
this.counterDownloaded.increment();
} else {
this.counterDownloadFailures.increment();
}
LOGGER.trace("Download result: " + ar.succeeded() + ", " + cloudStorage.mask(s3Path));
});
return promise.future();
}
private void cloudDownloadBlocking(Promise<Void> promise, String s3Path) {
try {
String localPath = this.cloudSync.toLocalPath(s3Path);
try (InputStream cloudInput = this.cloudStorage.download(s3Path)) {
this.localStorage.upload(cloudInput, localPath);
}
promise.complete();
} catch (Exception ex) {
// Be careful as the s3Path may contain the pre-signed S3 token, so do not log the whole path
LOGGER.error("download error: " + ex.getClass().getSimpleName());
promise.fail(new Throwable(ex));
}
}
private void handleAsyncResult(AsyncResult ar) {
if (ar.failed()) {
Throwable ex = ar.cause();
LOGGER.error(ex.getMessage(), ex);
}
}
}