Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed deposit retry bug fix #147

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.pass.deposit.DepositServiceErrorHandler;
import org.eclipse.pass.deposit.builder.DepositSubmissionModelBuilder;
import org.eclipse.pass.deposit.model.DepositFile;
import org.eclipse.pass.deposit.model.DepositSubmission;
Expand All @@ -48,14 +49,17 @@ public class FailedDepositRetry {
private final DepositSubmissionModelBuilder depositSubmissionModelBuilder;
private final DepositTaskHelper depositTaskHelper;
private final Registry<Packager> packagerRegistry;
private final DepositServiceErrorHandler depositServiceErrorHandler;

public FailedDepositRetry(PassClient passClient, DepositTaskHelper depositTaskHelper,
Registry<Packager> packagerRegistry,
DepositSubmissionModelBuilder depositSubmissionModelBuilder) {
DepositSubmissionModelBuilder depositSubmissionModelBuilder,
DepositServiceErrorHandler depositServiceErrorHandler) {
this.passClient = passClient;
this.depositTaskHelper = depositTaskHelper;
this.packagerRegistry = packagerRegistry;
this.depositSubmissionModelBuilder = depositSubmissionModelBuilder;
this.depositServiceErrorHandler = depositServiceErrorHandler;
}

public void retryFailedDeposit(Deposit failedDeposit) {
Expand Down Expand Up @@ -94,6 +98,7 @@ public void retryFailedDeposit(Deposit failedDeposit) {

} catch (Exception e) {
LOG.warn(FAILED_TO_PROCESS, failedDeposit.getId(), e.getMessage(), e);
depositServiceErrorHandler.handleError(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class DeploymentTestDataService {
private static final Logger LOG = LoggerFactory.getLogger(DeploymentTestDataService.class);

public static final String PASS_E2E_TEST_GRANT = "PASS_E2E_TEST_GRANT";
public static final String SUBMISSION_ID = "submission.id";

private final PassClient passClient;
private final DspaceDepositService dspaceDepositService;
Expand Down Expand Up @@ -94,19 +95,19 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException {
testSubmissions.forEach(testSubmission -> {
try {
PassClientSelector<Deposit> testDepositSelector = new PassClientSelector<>(Deposit.class);
testDepositSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
testDepositSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
testDepositSelector.setInclude("repositoryCopy");
List<Deposit> testDeposits = passClient.streamObjects(testDepositSelector).toList();
testDeposits.forEach(testDeposit -> {
deleteObject(testDeposit);
deleteObject(testDeposit.getRepositoryCopy());
});
PassClientSelector<File> testFileSelector = new PassClientSelector<>(File.class);
testFileSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
testFileSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
List<File> testFiles = passClient.streamObjects(testFileSelector).toList();
testFiles.forEach(this::deleteFile);
PassClientSelector<SubmissionEvent> subEventSelector = new PassClientSelector<>(SubmissionEvent.class);
subEventSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
subEventSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
List<SubmissionEvent> testSubmissionEvents = passClient.streamObjects(subEventSelector).toList();
testSubmissionEvents.forEach(this::deleteObject);
deleteObject(testSubmission);
Expand All @@ -120,8 +121,8 @@ private void deleteTestSubmissions(Grant testGrant) throws IOException {

private void deleteDepositsInRepoIfNeeded(Grant testGrant) throws IOException {
if (Boolean.FALSE.equals(skipDeploymentTestDeposits)) {
LOG.warn("Deleting Test Deposits In Repositories (skipDeploymentTestDeposits=" +
skipDeploymentTestDeposits + ")");
LOG.warn("Deleting Test Deposits In Repositories (skipDeploymentTestDeposits={})",
skipDeploymentTestDeposits);
ZonedDateTime submissionToDate = ZonedDateTime.now().minusHours(1);
PassClientSelector<Submission> testSubmissionSelector = new PassClientSelector<>(Submission.class);
testSubmissionSelector.setFilter(RSQL.and(
Expand All @@ -134,7 +135,7 @@ private void deleteDepositsInRepoIfNeeded(Grant testGrant) throws IOException {
testSubmissions.forEach(testSubmission -> {
try {
PassClientSelector<Deposit> testDepositSelector = new PassClientSelector<>(Deposit.class);
testDepositSelector.setFilter(RSQL.equals("submission.id", testSubmission.getId()));
testDepositSelector.setFilter(RSQL.equals(SUBMISSION_ID, testSubmission.getId()));
testDepositSelector.setInclude("submission", "repository", "repositoryCopy");
List<Deposit> testDeposits = passClient.streamObjects(testDepositSelector).toList();
testDeposits.forEach(testDeposit -> deleteDepositInRepoIfNeeded(testDeposit, authContext));
Expand All @@ -161,7 +162,9 @@ private boolean isDspaceDeposit(Deposit deposit) {

private void deleteObject(PassEntity entity) {
try {
passClient.deleteObject(entity);
if (Objects.nonNull(entity)) {
passClient.deleteObject(entity);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
public class DspaceDepositService {
private static final Logger LOG = LoggerFactory.getLogger(DspaceDepositService.class);

public static final String X_XSRF_TOKEN = "X-XSRF-TOKEN";
public static final String COOKIE = "Cookie";
public static final String DSPACE_XSRF_COOKIE = "DSPACE-XSRF-COOKIE=";
public static final String AUTHORIZATION = "Authorization";

private final RestClient restClient;

@Value("${dspace.user}")
Expand Down Expand Up @@ -109,12 +114,12 @@ AuthContext authenticate() {
ResponseEntity<Void> authResponse = restClient.post()
.uri("/authn/login")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header("X-XSRF-TOKEN", xsrfToken)
.header("Cookie", "DSPACE-XSRF-COOKIE=" + xsrfToken)
.header(X_XSRF_TOKEN, xsrfToken)
.header(COOKIE, DSPACE_XSRF_COOKIE + xsrfToken)
.body(bodyPair)
.retrieve()
.toBodilessEntity();
String authToken = getAuthHeaderValue(authResponse, "Authorization");
String authToken = getAuthHeaderValue(authResponse, AUTHORIZATION);

return new AuthContext(xsrfToken, authToken);
}
Expand All @@ -125,7 +130,9 @@ AuthContext authenticate() {
*/
void deleteDeposit(Deposit deposit, AuthContext authContext) {
LOG.warn("Deleting Test Deposit In Dspace (PASS Deposit ID={})", deposit.getId());
URI accessUrl = deposit.getRepositoryCopy().getAccessUrl();
URI accessUrl = Objects.nonNull(deposit.getRepositoryCopy())
? deposit.getRepositoryCopy().getAccessUrl()
: null;
LOG.warn("Deposit accessUrl={}", accessUrl);
if (Objects.nonNull(accessUrl)) {
String handleValue = parseHandleFilter(accessUrl);
Expand Down Expand Up @@ -168,7 +175,7 @@ private String findItemUuid(String handleValue, AuthContext authContext, String
String searchResponse = restClient.get()
.uri("/discover/search/objects?query=handle:{handleValue}&dsoType=item", handleValue)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", authContext.authToken())
.header(AUTHORIZATION, authContext.authToken())
.retrieve()
.body(String.class);
List<Map<String, ?>> searchArray = JsonPath.parse(searchResponse).read("$..indexableObject[?(@.handle)]");
Expand All @@ -195,7 +202,7 @@ private List<String> findBundleUuids(String itemUuid, AuthContext authContext)
String bundlesResponse = restClient.get()
.uri("/core/items/{itemUuid}/bundles", itemUuid)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", authContext.authToken())
.header(AUTHORIZATION, authContext.authToken())
.retrieve()
.body(String.class);
return JsonPath.parse(bundlesResponse).read("$..bundles[*].uuid");
Expand All @@ -206,9 +213,9 @@ private void deleteBundle(String bundleUuid, AuthContext authContext) {
restClient.delete()
.uri("/core/bundles/{bundleUuid}", bundleUuid)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", authContext.authToken())
.header("X-XSRF-TOKEN", authContext.xsrfToken())
.header("Cookie", "DSPACE-XSRF-COOKIE=" + authContext.xsrfToken())
.header(AUTHORIZATION, authContext.authToken())
.header(X_XSRF_TOKEN, authContext.xsrfToken())
.header(COOKIE, DSPACE_XSRF_COOKIE + authContext.xsrfToken())
.retrieve()
.toBodilessEntity();
LOG.warn("Deleted bundle UUID={}", bundleUuid);
Expand All @@ -219,9 +226,9 @@ private void deleteItem(String itemUuid, AuthContext authContext) {
restClient.delete()
.uri("/core/items/{itemUuid}", itemUuid)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", authContext.authToken())
.header("X-XSRF-TOKEN", authContext.xsrfToken())
.header("Cookie", "DSPACE-XSRF-COOKIE=" + authContext.xsrfToken())
.header(AUTHORIZATION, authContext.authToken())
.header(X_XSRF_TOKEN, authContext.xsrfToken())
.header(COOKIE, DSPACE_XSRF_COOKIE + authContext.xsrfToken())
.retrieve()
.toBodilessEntity();
LOG.warn("Deleted item UUID={}", itemUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package org.eclipse.pass.deposit.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;

Expand All @@ -40,12 +40,12 @@
/**
* @author Russ Poetker ([email protected])
*/
public class FailedDepositRetryIT extends AbstractDepositIT {
class FailedDepositRetryIT extends AbstractDepositIT {

@Autowired private DepositUpdater depositUpdater;

@Test
public void testFailedDepositRetry() throws Exception {
void testFailedDepositRetry_SuccessOnRetry() throws Exception {
// GIVEN
Submission submission = initFailedSubmissionDeposit();
mockSword();
Expand All @@ -68,12 +68,36 @@ public void testFailedDepositRetry() throws Exception {
RepositoryCopy popRepoCopy = passClient.getObject(updatedDeposit.getRepositoryCopy(),
"repository", "publication");
updatedDeposit.setRepositoryCopy(popRepoCopy);
verify(passClient).updateObject(eq(updatedDeposit));
verify(passClient).updateObject(updatedDeposit);
assertEquals(submission.getPublication().getId(), popRepoCopy.getPublication().getId());
assertEquals(1, submission.getRepositories().size());
assertEquals(submission.getRepositories().get(0).getId(), popRepoCopy.getRepository().getId());
}

@Test
void testFailedDepositRetry_FailOnRetry() throws Exception {
// GIVEN
Submission submission = initFailedSubmissionDeposit();

// WHEN
try {
depositUpdater.doUpdate();
} catch (IOException e) {
throw new RuntimeException(e);
}

// THEN
Condition<Set<Deposit>> actualDeposits = depositsForSubmission(submission.getId(), 1,
(deposit, repo) -> true);
assertTrue(actualDeposits.awaitAndVerify(deposits -> deposits.size() == 1 &&
DepositStatus.FAILED == deposits.iterator().next().getDepositStatus()));

Deposit actualDeposit = actualDeposits.getResult().iterator().next();
Deposit updatedDeposit = passClient.getObject(actualDeposit, "repositoryCopy");
assertNull(updatedDeposit.getRepositoryCopy());
verify(passClient).updateObject(updatedDeposit);
}

private Submission initFailedSubmissionDeposit() throws Exception {
Submission submission = findSubmission(createSubmission(
ResourceTestUtil.readSubmissionJson("sample2")));
Expand Down
Loading
Loading