Portability fixes - transactions and single save in transaction (#2133)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -22,7 +22,6 @@ import java.time.temporal.TemporalUnit;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -191,11 +190,14 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FINISHED: {
|
case FINISHED: {
|
||||||
handleFinishedAndStoreInTargetStatus(occurredAt, action).ifPresent(this::requestControllerAttributes);
|
requestControllerAttributes(handleFinishedAndStoreInTargetStatus(occurredAt, action));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case DOWNLOADED: {
|
case DOWNLOADED: {
|
||||||
handleDownloadedActionStatus(action).ifPresent(this::requestControllerAttributes);
|
handleDownloadedActionStatus(action).ifPresent(controllerId ->
|
||||||
|
requestControllerAttributes(getByControllerId(controllerId)
|
||||||
|
.map(JpaTarget.class::cast)
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@@ -282,8 +284,6 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
return addActionStatus((JpaActionStatusCreate) statusCreate);
|
return addActionStatus((JpaActionStatusCreate) statusCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Pageable PAGEABLE_1 = PageRequest.of(0, 1);
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Action> findActiveActionWithHighestWeight(final String controllerId) {
|
public Optional<Action> findActiveActionWithHighestWeight(final String controllerId) {
|
||||||
return Stream.concat(
|
return Stream.concat(
|
||||||
@@ -328,11 +328,8 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(isolation = Isolation.READ_COMMITTED)
|
@Transactional(isolation = Isolation.READ_COMMITTED)
|
||||||
@Retryable(retryFor = ConcurrencyFailureException.class, exclude = EntityAlreadyExistsException.class, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
@Retryable(retryFor = ConcurrencyFailureException.class, exclude = EntityAlreadyExistsException.class, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
|
||||||
public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, final URI address,
|
public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, final URI address, final String name, final String type) {
|
||||||
final String name, final String type) {
|
final Specification<JpaTarget> spec = (targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTarget_.controllerId), controllerId);
|
||||||
final Specification<JpaTarget> spec =
|
|
||||||
(targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTarget_.controllerId), controllerId);
|
|
||||||
|
|
||||||
return targetRepository.findOne(spec).map(target -> updateTarget(target, address, name, type))
|
return targetRepository.findOne(spec).map(target -> updateTarget(target, address, name, type))
|
||||||
.orElseGet(() -> createTarget(controllerId, address, name, type));
|
.orElseGet(() -> createTarget(controllerId, address, name, type));
|
||||||
}
|
}
|
||||||
@@ -845,10 +842,7 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
return Optional.of(target.getControllerId());
|
return Optional.of(target.getControllerId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestControllerAttributes(final String controllerId) {
|
private void requestControllerAttributes(final JpaTarget target) {
|
||||||
final JpaTarget target = (JpaTarget) getByControllerId(controllerId)
|
|
||||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
|
||||||
|
|
||||||
target.setRequestControllerAttributes(true);
|
target.setRequestControllerAttributes(true);
|
||||||
|
|
||||||
eventPublisherHolder.getEventPublisher()
|
eventPublisherHolder.getEventPublisher()
|
||||||
@@ -866,15 +860,13 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the case where the {@link Action.Status#FINISHED} status is
|
* Handles the case where the {@link Action.Status#FINISHED} status is reported by the device. In case the update is finished,
|
||||||
* reported by the device. In case the update is finished, a controllerId
|
* a controllerId will be returned to trigger a request for attributes.
|
||||||
* will be returned to trigger a request for attributes.
|
|
||||||
*
|
*
|
||||||
* @param action updated action
|
* @param action updated action
|
||||||
* @return a present controllerId in case the attributes needs to be
|
* @return a present controllerId in case the attributes needs to be requested.
|
||||||
* requested.
|
|
||||||
*/
|
*/
|
||||||
private Optional<String> handleFinishedAndStoreInTargetStatus(final long occurredAt, final JpaAction action) {
|
private JpaTarget handleFinishedAndStoreInTargetStatus(final long occurredAt, final JpaAction action) {
|
||||||
final JpaTarget target = (JpaTarget) action.getTarget();
|
final JpaTarget target = (JpaTarget) action.getTarget();
|
||||||
action.setActive(false);
|
action.setActive(false);
|
||||||
action.setStatus(Status.FINISHED);
|
action.setStatus(Status.FINISHED);
|
||||||
@@ -884,10 +876,8 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
target.setInstalledDistributionSet(ds);
|
target.setInstalledDistributionSet(ds);
|
||||||
target.setInstallationDate(occurredAt);
|
target.setInstallationDate(occurredAt);
|
||||||
|
|
||||||
// Target reported an installation of a DOWNLOAD_ONLY assignment, the
|
// Target reported an installation of a DOWNLOAD_ONLY assignment, the assigned DS has to be adapted
|
||||||
// assigned DS has to be adapted
|
// because the currently assigned DS can be unequal to the currently installed DS (the downloadOnly DS)
|
||||||
// because the currently assigned DS can be unequal to the currently
|
|
||||||
// installed DS (the downloadOnly DS)
|
|
||||||
if (isDownloadOnly(action)) {
|
if (isDownloadOnly(action)) {
|
||||||
target.setAssignedDistributionSet((JpaDistributionSet) action.getDistributionSet());
|
target.setAssignedDistributionSet((JpaDistributionSet) action.getDistributionSet());
|
||||||
}
|
}
|
||||||
@@ -899,11 +889,10 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
|||||||
target.setUpdateStatus(TargetUpdateStatus.IN_SYNC);
|
target.setUpdateStatus(TargetUpdateStatus.IN_SYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
targetRepository.save(target);
|
|
||||||
entityManager.detach(ds);
|
entityManager.detach(ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.of(target.getControllerId());
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertTargetAttributesQuota(final JpaTarget target) {
|
private void assertTargetAttributesQuota(final JpaTarget target) {
|
||||||
|
|||||||
@@ -343,7 +343,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
|||||||
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(testDs2.getId()))
|
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(testDs2.getId()))
|
||||||
.as("For newly created distributions sets the assigned target count should be zero").isFalse();
|
.as("For newly created distributions sets the assigned target count should be zero").isFalse();
|
||||||
|
|
||||||
Target target = createTargetWithAttributes("4711");
|
createTargetWithAttributes("4711");
|
||||||
|
|
||||||
final long current = System.currentTimeMillis();
|
final long current = System.currentTimeMillis();
|
||||||
controllerManagement.findOrRegisterTargetIfItDoesNotExist("4711", LOCALHOST);
|
controllerManagement.findOrRegisterTargetIfItDoesNotExist("4711", LOCALHOST);
|
||||||
@@ -356,7 +356,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
|||||||
assignDistributionSet(testDs2.getId(), "4711");
|
assignDistributionSet(testDs2.getId(), "4711");
|
||||||
implicitLock(testDs2);
|
implicitLock(testDs2);
|
||||||
|
|
||||||
target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new);
|
Target target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new);
|
||||||
// read data
|
// read data
|
||||||
|
|
||||||
assertThat(targetManagement.countByAssignedDistributionSet(testDs1.getId())).as("Target count is wrong")
|
assertThat(targetManagement.countByAssignedDistributionSet(testDs1.getId())).as("Target count is wrong")
|
||||||
@@ -1362,8 +1362,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
|||||||
targetManagement.create(entityFactory.target().create().controllerId(controllerId));
|
targetManagement.create(entityFactory.target().create().controllerId(controllerId));
|
||||||
final Target target = controllerManagement.updateControllerAttributes(controllerId, testData, null);
|
final Target target = controllerManagement.updateControllerAttributes(controllerId, testData, null);
|
||||||
|
|
||||||
assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong")
|
assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong").isEqualTo(testData);
|
||||||
.isEqualTo(testData);
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user