use of com.instaclustr.operations.Operation in project esop by instaclustr.
the class BaseBackupOperationCoordinator method coordinate.
@Override
public void coordinate(final Operation<BackupOperationRequest> operation) {
final BackupOperationRequest request = operation.request;
logger.info(request.toString());
try {
assert cassandraJMXService != null;
assert backuperFactoryMap != null;
assert bucketServiceFactoryMap != null;
assert objectMapper != null;
if (!request.skipBucketVerification) {
try (final BucketService bucketService = bucketServiceFactoryMap.get(request.storageLocation.storageProvider).createBucketService(request)) {
bucketService.checkBucket(request.storageLocation.bucket, request.createMissingBucket);
}
}
final CassandraData cassandraData = CassandraData.parse(request.dataDirs.get(0));
cassandraData.setDatabaseEntitiesFromRequest(request.entities);
final List<String> tokens = new CassandraTokens(cassandraJMXService).act();
logger.info("Tokens {}", tokens);
if (!Snapshots.snapshotContainsTimestamp(operation.request.snapshotTag)) {
if (operation.request.schemaVersion == null) {
operation.request.schemaVersion = new CassandraSchemaVersion(cassandraJMXService).act();
}
operation.request.snapshotTag = resolveSnapshotTag(operation.request, System.currentTimeMillis());
}
logger.info("Taking snapshot with name {}", request.snapshotTag);
new TakeSnapshotOperation(cassandraJMXService, new TakeSnapshotOperationRequest(request.entities, request.snapshotTag), cassandraVersionProvider).run0();
Snapshots.hashSpec = hashSpec;
final Snapshots snapshots = Snapshots.parse(request.dataDirs, request.snapshotTag);
final Optional<Snapshot> snapshot = snapshots.get(request.snapshotTag);
if (!snapshot.isPresent()) {
throw new IllegalStateException(format("There is not any snapshot of tag %s", request.snapshotTag));
}
final Manifest manifest = Manifest.from(snapshot.get());
manifest.setSchemaVersion(request.schemaVersion);
manifest.setTokens(tokens);
// manifest
final Path localManifestPath = getLocalManifestPath(request.snapshotTag);
Manifest.write(manifest, localManifestPath, objectMapper);
manifest.setManifest(getManifestAsManifestEntry(localManifestPath));
try (final Backuper backuper = backuperFactoryMap.get(request.storageLocation.storageProvider).createBackuper(request)) {
final List<ManifestEntry> manifestEntries = manifest.getManifestEntries();
Session<UploadUnit> uploadSession = null;
try {
uploadSession = uploadTracker.submit(backuper, operation, manifestEntries, request.snapshotTag, operation.request.concurrentConnections);
uploadSession.waitUntilConsideredFinished();
uploadTracker.cancelIfNecessary(uploadSession);
final List<UploadUnit> failedUnits = uploadSession.getFailedUnits();
if (!failedUnits.isEmpty()) {
final String message = failedUnits.stream().map(unit -> unit.getManifestEntry().objectKey.toString()).collect(Collectors.joining(","));
logger.error(message);
throw new IOException(format("Unable to upload some files successfully: %s", message));
}
} finally {
uploadTracker.removeSession(uploadSession);
uploadSession = null;
}
if (operation.request.uploadClusterTopology) {
// here we will upload all topology because we do not know what restore might look like (what dc a restorer will restore against if any)
final ClusterTopology topology = new CassandraClusterTopology(cassandraJMXService, null).act();
ClusterTopology.upload(backuper, topology, objectMapper, operation.request.snapshotTag);
}
} finally {
manifest.cleanup();
}
} catch (final Exception ex) {
operation.addError(Error.from(ex));
} finally {
final ClearSnapshotOperation cso = new ClearSnapshotOperation(cassandraJMXService, new ClearSnapshotOperationRequest(request.snapshotTag));
try {
cso.run0();
} catch (final Exception ex) {
operation.addErrors(cso.errors);
}
}
}
use of com.instaclustr.operations.Operation in project esop by instaclustr.
the class AbstractTracker method submit.
public synchronized Session<UNIT> submit(final INTERACTOR interactor, final Operation<? extends REQUEST> operation, final Collection<ManifestEntry> entries, final String snapshotTag, final int concurrentConnections) {
final Session<UNIT> currentSession = constructSession();
currentSession.setSnapshotTag(snapshotTag);
currentSession.setId(operation.id);
if (entries.isEmpty()) {
logger.info("0 files to process.");
return currentSession;
}
// we have executor service per request in order to specify maximal
// concurrent uploads, if we had one global executor, we could not "cap it".
final ListeningExecutorService executorService = new FixedTasksExecutorSupplier().get(concurrentConnections);
final Map<ListenableFuture<Void>, Unit> futures = new HashMap<>();
for (final ManifestEntry entry : entries) {
UNIT alreadySubmitted = null;
final Iterator<UNIT> it = Collections.unmodifiableList(new ArrayList<>(units)).iterator();
while (it.hasNext()) {
UNIT unit = it.next();
if (unit.getManifestEntry().objectKey.equals(entry.objectKey)) {
alreadySubmitted = unit;
break;
}
}
if (alreadySubmitted == null) {
final UNIT unit = constructUnitToSubmit(interactor, entry, operation.getShouldCancel(), snapshotTag, hashSpec);
units.add(unit);
futures.put(executorService.submit(unit), unit);
submittedUnits.incrementAndGet();
currentSession.addUnit(unit);
} else {
logger.info(String.format("Session %s skips as already submitted: %s", currentSession.getId(), alreadySubmitted.getManifestEntry().objectKey));
currentSession.addUnit(alreadySubmitted);
}
}
sessions.add(currentSession);
submittedSessions.incrementAndGet();
futures.forEach((key, value) -> key.addListener(() -> {
synchronized (sessions) {
// increment finished units across all sessions
sessions.stream().filter(s -> s.getUnits().contains(value)).forEach(s -> {
operationsService.operation(s.getId()).ifPresent(op -> {
s.finishedUnits.incrementAndGet();
logger.debug(String.format("Progress of operation %s: %s", op.id, s.getProgress()));
op.progress = s.getProgress();
});
});
units.remove(value);
}
}, finisherExecutorService));
currentSession.setExecutorService(executorService);
return currentSession;
}
Aggregations