Skip to content

Commit 1a8caa2

Browse files
author
Hans-Peter Klett
committed
Added job recovery helpers and fixed /buckets
1 parent 2b3aa35 commit 1a8caa2

File tree

8 files changed

+112
-26
lines changed

8 files changed

+112
-26
lines changed

src/main/java/com/spectralogic/ds3client/commands/BulkRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public long getSize() {
6565

6666
@Override
6767
public String getPath() {
68-
return "/_rest_/buckets/" + this.bucket;
68+
return "/_rest_/bucket/" + this.bucket;
6969
}
7070

7171
@Override

src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpers.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ public abstract Ds3ClientHelpers.ReadJob startReadJob(final String bucket, final
150150
public abstract Ds3ClientHelpers.ReadJob startReadAllJob(final String bucket)
151151
throws SignatureException, IOException, XmlProcessingException;
152152

153+
/**
154+
* Queries job information based on job id and returns a {@link ReadJob} that can resume the job.
155+
* @throws SignatureException
156+
* @throws IOException
157+
* @throws XmlProcessingException
158+
* @throws JobRecoveryException
159+
*/
160+
public abstract Ds3ClientHelpers.WriteJob recoverWriteJob(final UUID jobId)
161+
throws SignatureException, IOException, XmlProcessingException, JobRecoveryException;
162+
163+
/**
164+
* Queries job information based on job id and returns a {@link WriteJob} that can resume the job.
165+
* @throws SignatureException
166+
* @throws IOException
167+
* @throws XmlProcessingException
168+
* @throws JobRecoveryException
169+
*/
170+
public abstract Ds3ClientHelpers.ReadJob recoverReadJob(final UUID jobId)
171+
throws SignatureException, IOException, XmlProcessingException, JobRecoveryException;
172+
153173
/**
154174
* Ensures that a bucket exists. The the bucket does not exist, it will be created.
155175
* @param bucket The name of the bucket to check that it exists.

src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpersImpl.java

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515

1616
package com.spectralogic.ds3client.helpers;
1717

18-
import com.google.common.collect.Lists;
19-
import com.spectralogic.ds3client.Ds3Client;
20-
import com.spectralogic.ds3client.commands.*;
21-
import com.spectralogic.ds3client.models.Contents;
22-
import com.spectralogic.ds3client.models.Ds3Object;
23-
import com.spectralogic.ds3client.models.ListBucketResult;
24-
import com.spectralogic.ds3client.models.MasterObjectList;
25-
import com.spectralogic.ds3client.serializer.XmlProcessingException;
26-
2718
import java.io.IOException;
2819
import java.nio.file.FileVisitResult;
2920
import java.nio.file.Files;
@@ -33,10 +24,19 @@
3324
import java.security.SignatureException;
3425
import java.util.ArrayList;
3526
import java.util.List;
27+
import java.util.UUID;
28+
29+
import com.google.common.collect.Lists;
30+
import com.spectralogic.ds3client.Ds3Client;
31+
import com.spectralogic.ds3client.commands.*;
32+
import com.spectralogic.ds3client.models.*;
33+
import com.spectralogic.ds3client.serializer.XmlProcessingException;
3634

3735
class Ds3ClientHelpersImpl extends Ds3ClientHelpers {
3836

3937
private static final int DEFAULT_MAX_KEYS = 1000;
38+
private static final String JOB_TYPE_PUT = "PUT";
39+
private static final String JOB_TYPE_GET = "GET";
4040
private final Ds3Client client;
4141

4242
Ds3ClientHelpersImpl(final Ds3Client client) {
@@ -74,11 +74,59 @@ public Ds3ClientHelpers.ReadJob startReadAllJob(final String bucket)
7474
return this.startReadJob(bucket, ds3Objects);
7575
}
7676

77+
@Override
78+
public WriteJob recoverWriteJob(final UUID jobId) throws SignatureException, IOException, XmlProcessingException, JobRecoveryException {
79+
try (final GetJobResponse job = this.client.getJob(new GetJobRequest(jobId))) {
80+
final JobInfo jobInfo = job.getJobInfo();
81+
checkJobType(JOB_TYPE_PUT, jobInfo.getRequestType());
82+
return new WriteJobImpl(this.client, jobInfo.getJobId(), jobInfo.getBucketName(), job.getObjectsList());
83+
}
84+
}
85+
86+
@Override
87+
public ReadJob recoverReadJob(final UUID jobId) throws SignatureException, IOException, XmlProcessingException, JobRecoveryException {
88+
try (final GetJobResponse job = this.client.getJob(new GetJobRequest(jobId))) {
89+
final JobInfo jobInfo = job.getJobInfo();
90+
checkJobType(JOB_TYPE_GET, jobInfo.getRequestType());
91+
return new ReadJobImpl(this.client, jobInfo.getJobId(), jobInfo.getBucketName(), convertFromJobObjectsList(job.getObjectsList()));
92+
}
93+
}
94+
95+
private static List<Objects> convertFromJobObjectsList(final List<JobObjects> jobObjectsList) {
96+
final List<Objects> objectsList = new ArrayList<>(jobObjectsList.size());
97+
for (final JobObjects jobObjects : jobObjectsList) {
98+
objectsList.add(convertFromJobObjects(jobObjects));
99+
}
100+
return objectsList;
101+
}
102+
103+
private static Objects convertFromJobObjects(final JobObjects jobObjects) {
104+
final Objects objects = new Objects();
105+
objects.setServerId(jobObjects.getServerId());
106+
objects.setObject(concat(jobObjects.getObjectsInCache(), jobObjects.getObject()));
107+
return objects;
108+
}
109+
110+
@SafeVarargs
111+
private static <T> List<T> concat(final List<T>... lists) {
112+
final List<T> result = new ArrayList<>();
113+
for (final List<T> list : lists) {
114+
result.addAll(list);
115+
}
116+
return result;
117+
}
118+
119+
private static void checkJobType(final String expectedJobType, final String actualJobType) throws JobRecoveryException {
120+
if (!actualJobType.equals(expectedJobType)) {
121+
throw new JobRecoveryException(expectedJobType, actualJobType);
122+
}
123+
}
124+
77125
@Override
78126
public void ensureBucketExists(final String bucket) throws IOException, SignatureException {
79-
try (final HeadBucketResponse response = client.headBucket(new HeadBucketRequest(bucket))) {
127+
try (final HeadBucketResponse response = this.client.headBucket(new HeadBucketRequest(bucket))) {
80128
if (response.getStatus() == HeadBucketResponse.Status.DOESNTEXIST) {
81-
client.putBucket(new PutBucketRequest(bucket)).close();
129+
this.client.putBucket(new PutBucketRequest(bucket)).close();
82130
}
83131
}
84132
}

src/main/java/com/spectralogic/ds3client/helpers/JobImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ abstract class JobImpl implements Job {
4040
private final Ds3Client client;
4141
private final UUID jobId;
4242
private final String bucketName;
43-
private final Iterable<Objects> objectLists;
43+
private final Iterable<? extends Objects> objectLists;
4444

4545
//TODO: client factory
4646
public JobImpl(
4747
final Ds3Client client,
4848
final UUID jobId,
4949
final String bucketName,
50-
final Iterable<Objects> objectLists) {
50+
final Iterable<? extends Objects> objectLists) {
5151
this.client = client;
5252
this.jobId = jobId;
5353
this.bucketName = bucketName;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.helpers;
17+
18+
public class JobRecoveryException extends Exception {
19+
private static final long serialVersionUID = -4418169724222972364L;
20+
21+
JobRecoveryException(final String expectedType, final String actualType) {
22+
super(buildMessage(expectedType, actualType));
23+
}
24+
25+
private static String buildMessage(final String expectedType, final String actualType) {
26+
return String.format("Expected job type '%s' but the actual job was of type '%s'.", expectedType, actualType);
27+
}
28+
}

src/main/java/com/spectralogic/ds3client/helpers/ReadJobImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ReadJobImpl(
3636
final Ds3Client client,
3737
final UUID jobId,
3838
final String bucketName,
39-
final Iterable<Objects> objectLists) {
39+
final Iterable<? extends Objects> objectLists) {
4040
super(client, jobId, bucketName, objectLists);
4141
}
4242

src/main/java/com/spectralogic/ds3client/helpers/WriteJobImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public WriteJobImpl(
3535
final Ds3Client client,
3636
final UUID jobId,
3737
final String bucketName,
38-
final Iterable<Objects> objectLists) {
38+
final Iterable<? extends Objects> objectLists) {
3939
super(client, jobId, bucketName, objectLists);
4040
}
4141

src/test/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpers_Test.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.UUID;
2929

3030
import org.apache.commons.io.IOUtils;
31-
import org.hamcrest.Description;
3231
import org.junit.Assert;
3332
import org.junit.Test;
3433
import org.mockito.ArgumentMatcher;
@@ -199,15 +198,6 @@ public boolean matches(final Object argument) {
199198
&& putObjectRequest.getObjectName().equals(key)
200199
&& streamToString(stream).equals(contents);
201200
}
202-
203-
@Override
204-
public void describeTo(final Description description) {
205-
description.appendText("PutObjectRequest(\"");
206-
description.appendText(key);
207-
description.appendText("\", \"");
208-
description.appendText(contents);
209-
description.appendText("\")");
210-
}
211201
});
212202
}
213203

0 commit comments

Comments
 (0)