Feature do not temp store uploaded files (#763)

* Upload artefacts directly without creating a temp file
* Remove redundant code that checks if user tries to update a directory
* Handle UI upload exceptions, use Spring thread pool
* Clean up code
* Fix review findings
* Fix Sonar findings

Signed-off-by: Stefan Klotz <stefan.klotz@bosch-si.com>
Signed-off-by: Stefan Behl <stefan.behl@bosch-si.com>
This commit is contained in:
Stefan Klotz
2018-11-12 08:57:10 +01:00
committed by Stefan Behl
parent 49d064df18
commit d4c1e82090
11 changed files with 266 additions and 137 deletions

View File

@@ -25,9 +25,7 @@ public class TargetWithActionType {
private String maintenanceWindowTimeZone;
public TargetWithActionType(final String controllerId) {
this.controllerId = controllerId;
this.actionType = ActionType.FORCED;
this.forceTime = 0;
this(controllerId, ActionType.FORCED, 0);
}
public TargetWithActionType(final String controllerId, final ActionType actionType, final long forceTime) {
@@ -115,4 +113,13 @@ public class TargetWithActionType {
public String getMaintenanceWindowTimeZone() {
return maintenanceWindowTimeZone;
}
@Override
public String toString() {
return "TargetWithActionType [controllerId=" + controllerId + ", actionType=" + getActionType() + ", forceTime="
+ getForceTime() + ", maintenanceSchedule=" + getMaintenanceSchedule() + ", maintenanceWindowDuration="
+ getMaintenanceWindowDuration() + ", maintenanceWindowTimeZone=" + getMaintenanceWindowTimeZone()
+ "]";
}
}

View File

@@ -215,6 +215,7 @@ public abstract class AbstractDsAssignmentStrategy {
.publishEvent(new CancelTargetAssignmentEvent(target, actionId, applicationContext.getId())));
}
@SuppressWarnings("squid:S2259")
JpaAction createTargetAction(final Map<String, TargetWithActionType> targetsWithActionMap, final JpaTarget target,
final JpaDistributionSet set) {

View File

@@ -90,6 +90,9 @@ import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
@@ -100,6 +103,7 @@ import com.google.common.collect.Lists;
@Transactional(readOnly = true)
@Validated
public class JpaDeploymentManagement implements DeploymentManagement {
private static final Logger LOG = LoggerFactory.getLogger(JpaDeploymentManagement.class);
/**
@@ -321,8 +325,10 @@ public class JpaDeploymentManagement implements DeploymentManagement {
assignmentStrategy.updateTargetStatus(set, targetIds, currentUser);
final Map<String, JpaAction> targetIdsToActions = targets.stream()
.map(t -> actionRepository.save(assignmentStrategy.createTargetAction(targetsWithActionMap, t, set)))
.collect(Collectors.toMap(a -> a.getTarget().getControllerId(), Function.identity()));
.map(trg -> createTargetAction(assignmentStrategy, targetsWithActionType, controllerIDs, targets,
targetsWithActionMap, trg, set))
.map(actionRepository::save)
.collect(Collectors.toMap(action -> action.getTarget().getControllerId(), Function.identity()));
// create initial action status when action is created so we remember
// the initial running status because we will change the status
@@ -578,7 +584,6 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Page<Action> findActiveActionsByTarget(final Pageable pageable, final String controllerId) {
throwExceptionIfTargetDoesNotExist(controllerId);
return actionRepository.findByActiveAndTarget(pageable, controllerId, true);
}
@@ -592,14 +597,12 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public long countActionsByTarget(final String controllerId) {
throwExceptionIfTargetDoesNotExist(controllerId);
return actionRepository.countByTargetControllerId(controllerId);
}
@Override
public long countActionsByTarget(final String rsqlParam, final String controllerId) {
throwExceptionIfTargetDoesNotExist(controllerId);
return actionRepository.count(createSpecificationFor(controllerId, rsqlParam));
}
@@ -684,7 +687,6 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Slice<Action> findActionsByDistributionSet(final Pageable pageable, final long dsId) {
throwExceptionIfDistributionSetDoesNotExist(dsId);
return actionRepository.findByDistributionSetId(pageable, dsId);
}
@@ -696,14 +698,12 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Optional<DistributionSet> getAssignedDistributionSet(final String controllerId) {
throwExceptionIfTargetDoesNotExist(controllerId);
return distributionSetRepository.findAssignedToTarget(controllerId);
}
@Override
public Optional<DistributionSet> getInstalledDistributionSet(final String controllerId) {
throwExceptionIfTargetDoesNotExist(controllerId);
return distributionSetRepository.findInstalledAtTarget(controllerId);
}
@@ -736,7 +736,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
return deleteQuery.executeUpdate();
}
private static String getQueryForDeleteActionsByStatusAndLastModifiedBeforeString(Database database) {
private static String getQueryForDeleteActionsByStatusAndLastModifiedBeforeString(final Database database) {
return QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED.getOrDefault(database,
QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED_DEFAULT);
}
@@ -749,4 +749,52 @@ public class JpaDeploymentManagement implements DeploymentManagement {
return "#" + Joiner.on(",#").join(elements);
}
@SuppressWarnings({ "squid:S1695", "squid:S1696" })
private JpaAction createTargetAction(final AbstractDsAssignmentStrategy assignmentStrategy,
final Collection<TargetWithActionType> targetsWithActionType, final List<String> controllerIDs,
final List<JpaTarget> targets, final Map<String, TargetWithActionType> targetsWithActionMap,
final JpaTarget target, final JpaDistributionSet set) {
try {
return assignmentStrategy.createTargetAction(targetsWithActionMap, target, set);
} catch (final NullPointerException e) {
dumpDistributionSetAssignment(target, set, targetsWithActionType, controllerIDs, targets,
targetsWithActionMap);
throw e;
}
}
private void dumpDistributionSetAssignment(final JpaTarget target, final JpaDistributionSet set,
final Collection<TargetWithActionType> targetsWithActionType, final List<String> controllerIDs,
final List<JpaTarget> targets, final Map<String, TargetWithActionType> targetsWithActionMap) {
final ObjectWriter writer = new ObjectMapper().writer();
final String correlationID = "DistributionSetAssignmentDump[" + Thread.currentThread().getId() + "]";
dump(correlationID, "Target", Objects.toString(target));
dump(correlationID, "DistributionSet", Objects.toString(set));
if (target != null) {
dump(correlationID, "ControllerID", Objects.toString(target.getControllerId()));
if (set != null) {
dump(correlationID, "Actions", toString(writer,
actionRepository.findByTargetAndDistributionSet(new PageRequest(0, 500), target, set)));
}
}
dump(correlationID, "ControllerIDs", toString(writer, controllerIDs));
dump(correlationID, "Targets", toString(writer, targets));
dump(correlationID, "TargetsWithActionType", toString(writer, targetsWithActionType));
dump(correlationID, "TargetsWithActionMap", toString(writer, targetsWithActionMap));
}
private static void dump(final String prefix, final String key, final String value) {
LOG.error("{} >>> {}: {}", prefix, key, value);
}
@SuppressWarnings("squid:S1166")
private static String toString(final ObjectWriter writer, final Object o) {
try {
return writer.writeValueAsString(o);
} catch (final JsonProcessingException e) {
LOG.error("Object serialization failed", e);
return e.getMessage();
}
}
}

View File

@@ -209,7 +209,8 @@ public class JpaAction extends AbstractJpaTenantAwareBaseEntity implements Actio
@Override
public String toString() {
return "JpaAction [distributionSet=" + distributionSet.getId() + ", version=" + getOptLockRevision() + ", id="
+ getId() + "]";
+ getId() + ", actionType=" + getActionType() + ", isActive=" + isActive() + ", createdAt="
+ getCreatedAt() + ", lastModifiedAt=" + getLastModifiedAt() + "]";
}
@Override