Skip to content

Commit

Permalink
add size field to manifest and list only manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklosovic committed Oct 3, 2023
1 parent 327f3be commit 2ce3458
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/instaclustr/esop/impl/Manifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class Manifest implements Cloneable {

private String schemaVersion;

private long size;

public static Manifest from(final Snapshot snapshot) {
return new Manifest(snapshot);
}
Expand Down Expand Up @@ -91,6 +93,14 @@ public List<String> getTokens() {
return tokens;
}

public void setSize(long size) {
this.size = size;
}

public long getSize() {
return this.size;
}

@JsonIgnore
public String getInitialTokensCassandraYamlFragment() {
return "initial_token: " + String.join(",", getTokens());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public StorageInteractor(final StorageLocation storageLocation) {
this.storageLocation = storageLocation;
}

public String resolveNodeAwareRemoteRoot() {
return Paths.get(storageLocation.clusterId).resolve(storageLocation.datacenterId).resolve(storageLocation.nodeId).toString();
}

public String resolveNodeAwareRemotePath(final Path objectKey) {
return Paths.get(storageLocation.clusterId).resolve(storageLocation.datacenterId).resolve(storageLocation.nodeId).resolve(objectKey).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void coordinate(final Operation<BackupOperationRequest> operation) {
final Snapshots snapshots = Snapshots.parse(request.dataDirs, request.snapshotTag);
final Optional<Snapshot> snapshot = snapshots.get(request.snapshotTag);

if (!snapshot.isPresent()) {
if (snapshot.isEmpty()) {
throw new IllegalStateException(format("There is not any snapshot of tag %s", request.snapshotTag));
}

Expand All @@ -150,7 +150,7 @@ public void coordinate(final Operation<BackupOperationRequest> operation) {
backuper.init(manifest.getManifestEntries(true));
performUpload(manifest.getManifestEntries(false), backuper, operation, request);

// upload manifest as the last, possibly with updated file sizes as they were encrypted
manifest.setSize(manifest.getManifestEntries(true).stream().map(m -> m.size).reduce(Long::sum).orElse(0L));
backuper.uploadText(objectMapper.writeValueAsString(manifest),
backuper.objectKeyToNodeAwareRemoteReference(manifest.getManifest().objectKey));

Expand Down
37 changes: 24 additions & 13 deletions src/main/java/com/instaclustr/esop/s3/v2/BaseS3Backuper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
import software.amazon.awssdk.services.s3.model.CompletedPart;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesResponse;
import software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.ListMultipartUploadsRequest;
import software.amazon.awssdk.services.s3.model.ListMultipartUploadsResponse;
import software.amazon.awssdk.services.s3.model.MultipartUpload;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.s3.model.NoSuchUploadException;
import software.amazon.awssdk.services.s3.model.ObjectAttributes;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.SdkPartType;
import software.amazon.awssdk.services.s3.model.StorageClass;
Expand Down Expand Up @@ -265,10 +268,10 @@ private void uploadFile(S3Client s3Client,
if (!completeResponse.sdkHttpResponse().isSuccessful()) {
throw new RuntimeException(String.format("Unsuccessful multipart upload of %s, upload id %s", objectReference.canonicalPath, uploadId));
} else {
logger.info("Completed multipart upload of {}, upload id {}, etag {}", objectReference.canonicalPath, uploadId, completeResponse.eTag());
logger.debug("Completed multipart upload of {}, upload id {}, etag {}", objectReference.canonicalPath, uploadId, completeResponse.eTag());
}

logger.info("Waiting for " + objectReference.canonicalPath + " to exist");
logger.debug("Waiting for " + objectReference.canonicalPath + " to exist");

s3Clients.getNonEncryptingClient().waiter().waitUntilObjectExists(HeadObjectRequest.builder()
.bucket(request.storageLocation.bucket)
Expand All @@ -278,17 +281,25 @@ private void uploadFile(S3Client s3Client,
.waitTimeout(Duration.of(1, ChronoUnit.MINUTES))
.build());

logger.info("Object under key " + objectReference.canonicalPath + " exists");

// GetObjectAttributesResponse objectAttributes = s3Clients.getNonEncryptingClient()
// .getObjectAttributes(GetObjectAttributesRequest
// .builder()
// .bucket(request.storageLocation.bucket)
// .key(objectReference.canonicalPath)
// .objectAttributes(ObjectAttributes.OBJECT_SIZE)
// .build());
//
// manifestEntry.size = objectAttributes.objectSize();
logger.debug("Object under key " + objectReference.canonicalPath + " exists");

if (s3Clients.hasEncryptingClient()) {
try {
GetObjectAttributesResponse objectAttributes = s3Clients.getNonEncryptingClient()
.getObjectAttributes(GetObjectAttributesRequest
.builder()
.bucket(request.storageLocation.bucket)
.key(objectReference.canonicalPath)
.objectAttributes(ObjectAttributes.OBJECT_SIZE)
.build());

manifestEntry.size = objectAttributes.objectSize();
}
catch (Throwable t) {
logger.warn("Unable to get attribute {} for key {} by GetObjectAttributes request. Please check your permissions.",
ObjectAttributes.OBJECT_SIZE, objectReference.canonicalPath);
}
}
} catch (Throwable t) {
t.printStackTrace();
multipartAbortionService.abortMultipartUpload(uploadId, request, objectReference);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/instaclustr/esop/s3/v2/BaseS3Restorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ public static List<List<String>> splitList(List<String> list, int maxLength) {
public void downloadManifestsToDirectory(Path downloadDir) throws Exception {
FileUtils.createDirectory(downloadDir);
FileUtils.cleanDirectory(downloadDir.toFile());
final List<S3Object> manifestSumms = listBucket("", s -> s.contains("manifests"));

final List<S3Object> manifestSumms = listBucket(resolveNodeAwareRemotePath(Paths.get("manifests")), filter -> true);
for (S3Object o : manifestSumms) {
Path manifestPath = Paths.get(o.key());
Path destination = downloadDir.resolve(manifestPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AzureListingAndBackupRemovalTest extends BaseListingRemovalTest {

@Override
protected List<Module> getModules() {
return new ArrayList<Module>() {{
return new ArrayList<>() {{
add(new AzureModule());
}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class GoogleListingAndBackupRemovalTest extends BaseListingRemovalTest {

@Override
protected List<Module> getModules() {
return new ArrayList<Module>() {{
return new ArrayList<>() {{
add(new GCPModule());
}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class LocalListingAndBackupRemovalTest extends BaseListingRemovalTest {

@Override
protected List<Module> getModules() {
return new ArrayList<Module>() {{
return new ArrayList<>() {{
add(new LocalFileModule());
}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class S3ListingAndBackupRemovalTest extends BaseListingRemovalTest {

@Override
protected List<Module> getModules() {
return new ArrayList<Module>() {{
return new ArrayList<>() {{
add(new S3Module());
}};
}
Expand Down

0 comments on commit 2ce3458

Please sign in to comment.