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

Catch more SQL exceptions in persister queries #2038

Merged
merged 2 commits into from
Nov 19, 2019
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
@@ -1,5 +1,6 @@
package com.hubspot.singularity.data.history;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand All @@ -11,6 +12,7 @@
import org.slf4j.LoggerFactory;

import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
import com.hubspot.singularity.DeployState;
import com.hubspot.singularity.ExtendedTaskState;
Expand Down Expand Up @@ -101,8 +103,16 @@ public void saveRequestHistoryUpdate(SingularityRequestHistory requestHistory) {
LOG.trace("saveRequestHistoryUpdate requestHistory {}", requestHistory);
}

history.insertRequestHistory(requestHistory.getRequest().getId(), requestHistory.getRequest(), new Date(requestHistory.getCreatedAt()),
requestHistory.getEventType().name(), getUserField(requestHistory.getUser()), getMessageField(requestHistory.getMessage()));
try {
history.insertRequestHistory(requestHistory.getRequest().getId(), requestHistory.getRequest(), new Date(requestHistory.getCreatedAt()),
requestHistory.getEventType().name(), getUserField(requestHistory.getUser()), getMessageField(requestHistory.getMessage()));
} catch (Throwable t) {
if (Throwables.getCausalChain(t).stream().anyMatch((exn) -> exn instanceof SQLIntegrityConstraintViolationException)) {
LOG.info("Entry for {} ({}) already existed, skipping save", requestHistory.getRequest(), requestHistory.getCreatedAt());
} else {
throw t;
}
}
}

@Override
Expand All @@ -111,14 +121,22 @@ public void saveDeployHistory(SingularityDeployHistory deployHistory) {
LOG.trace("saveDeployHistory {}", deployHistory);
}

history.insertDeployHistory(deployHistory.getDeployMarker().getRequestId(),
deployHistory.getDeployMarker().getDeployId(),
new Date(deployHistory.getDeployMarker().getTimestamp()),
getUserField(deployHistory.getDeployMarker().getUser()),
getMessageField(deployHistory.getDeployMarker().getMessage()),
deployHistory.getDeployResult().isPresent() ? new Date(deployHistory.getDeployResult().get().getTimestamp()) : new Date(deployHistory.getDeployMarker().getTimestamp()),
deployHistory.getDeployResult().isPresent() ? deployHistory.getDeployResult().get().getDeployState().name() : DeployState.CANCELED.name(),
deployHistory);
try {
history.insertDeployHistory(deployHistory.getDeployMarker().getRequestId(),
deployHistory.getDeployMarker().getDeployId(),
new Date(deployHistory.getDeployMarker().getTimestamp()),
getUserField(deployHistory.getDeployMarker().getUser()),
getMessageField(deployHistory.getDeployMarker().getMessage()),
deployHistory.getDeployResult().isPresent() ? new Date(deployHistory.getDeployResult().get().getTimestamp()) : new Date(deployHistory.getDeployMarker().getTimestamp()),
deployHistory.getDeployResult().isPresent() ? deployHistory.getDeployResult().get().getDeployState().name() : DeployState.CANCELED.name(),
deployHistory);
} catch (Throwable t) {
if (Throwables.getCausalChain(t).stream().anyMatch((exn) -> exn instanceof SQLIntegrityConstraintViolationException)) {
LOG.info("Entry for {} - {} already existed, skipping save", deployHistory.getDeployMarker().getRequestId(), deployHistory.getDeployMarker().getDeployId());
} else {
throw t;
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ public void testMessage() {
}
}

@Test
public void testDuplicatePersist() {
initRequest();
requestResource.scale(requestId, new SingularityScaleRequest(Optional.of(2), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of("msg"), Optional.empty(), Optional.empty(), Optional.empty()), singularityUser);
requestHistoryPersister.runActionOnPoll();
SingularityRequestHistory history = historyManager.getRequestHistory(requestId, Optional.of(OrderDirection.DESC), 0, 100).get(0);
historyManager.saveRequestHistoryUpdate(history);
// Should not throw exception
historyManager.saveRequestHistoryUpdate(history);
}

@Test
public void testMigrateToJson() throws IOException {
try(Handle handle = dbiProvider.get().open()) {
Expand Down