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

Portability fixes - transactions and single save in transaction #2133

Merged
merged 1 commit into from
Dec 10, 2024
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,7 +22,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -191,11 +190,14 @@ protected void onActionStatusUpdate(final JpaActionStatus newActionStatus, final
break;
}
case FINISHED: {
handleFinishedAndStoreInTargetStatus(occurredAt, action).ifPresent(this::requestControllerAttributes);
requestControllerAttributes(handleFinishedAndStoreInTargetStatus(occurredAt, action));
break;
}
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;
}
default: {
Expand Down Expand Up @@ -282,8 +284,6 @@ public Action addUpdateActionStatus(final ActionStatusCreate statusCreate) {
return addActionStatus((JpaActionStatusCreate) statusCreate);
}

private static final Pageable PAGEABLE_1 = PageRequest.of(0, 1);

@Override
public Optional<Action> findActiveActionWithHighestWeight(final String controllerId) {
return Stream.concat(
Expand Down Expand Up @@ -328,11 +328,8 @@ public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, fi
@Override
@Transactional(isolation = Isolation.READ_COMMITTED)
@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,
final String name, final String type) {
final Specification<JpaTarget> spec =
(targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTarget_.controllerId), controllerId);

public Target findOrRegisterTargetIfItDoesNotExist(final String controllerId, final URI address, final String name, final String type) {
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))
.orElseGet(() -> createTarget(controllerId, address, name, type));
}
Expand Down Expand Up @@ -845,10 +842,7 @@ private Optional<String> handleDownloadedActionStatus(final JpaAction action) {
return Optional.of(target.getControllerId());
}

private void requestControllerAttributes(final String controllerId) {
final JpaTarget target = (JpaTarget) getByControllerId(controllerId)
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));

private void requestControllerAttributes(final JpaTarget target) {
target.setRequestControllerAttributes(true);

eventPublisherHolder.getEventPublisher()
Expand All @@ -866,15 +860,13 @@ private void handleErrorOnAction(final JpaAction mergedAction, final JpaTarget m
}

/**
* Handles the case where the {@link Action.Status#FINISHED} status is
* reported by the device. In case the update is finished, a controllerId
* will be returned to trigger a request for attributes.
* Handles the case where the {@link Action.Status#FINISHED} status is reported by the device. In case the update is finished,
* a controllerId will be returned to trigger a request for attributes.
*
* @param action updated action
* @return a present controllerId in case the attributes needs to be
* requested.
* @return a present controllerId in case the attributes needs to be 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();
action.setActive(false);
action.setStatus(Status.FINISHED);
Expand All @@ -884,10 +876,8 @@ private Optional<String> handleFinishedAndStoreInTargetStatus(final long occurre
target.setInstalledDistributionSet(ds);
target.setInstallationDate(occurredAt);

// Target reported an installation of a DOWNLOAD_ONLY assignment, the
// assigned DS has to be adapted
// because the currently assigned DS can be unequal to the currently
// installed DS (the downloadOnly DS)
// Target reported an installation of a DOWNLOAD_ONLY assignment, the assigned DS has to be adapted
// because the currently assigned DS can be unequal to the currently installed DS (the downloadOnly DS)
if (isDownloadOnly(action)) {
target.setAssignedDistributionSet((JpaDistributionSet) action.getDistributionSet());
}
Expand All @@ -899,11 +889,10 @@ private Optional<String> handleFinishedAndStoreInTargetStatus(final long occurre
target.setUpdateStatus(TargetUpdateStatus.IN_SYNC);
}

targetRepository.save(target);
entityManager.detach(ds);
}

return Optional.of(target.getControllerId());
return target;
}

private void assertTargetAttributesQuota(final JpaTarget target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void findTargetByControllerIDWithDetails() {
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(testDs2.getId()))
.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();
controllerManagement.findOrRegisterTargetIfItDoesNotExist("4711", LOCALHOST);
Expand All @@ -356,7 +356,7 @@ void findTargetByControllerIDWithDetails() {
assignDistributionSet(testDs2.getId(), "4711");
implicitLock(testDs2);

target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new);
Target target = targetManagement.getByControllerID("4711").orElseThrow(IllegalStateException::new);
// read data

assertThat(targetManagement.countByAssignedDistributionSet(testDs1.getId())).as("Target count is wrong")
Expand Down Expand Up @@ -1362,8 +1362,7 @@ private Target createTargetWithAttributes(final String controllerId) {
targetManagement.create(entityFactory.target().create().controllerId(controllerId));
final Target target = controllerManagement.updateControllerAttributes(controllerId, testData, null);

assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong")
.isEqualTo(testData);
assertThat(targetManagement.getControllerAttributes(controllerId)).as("Controller Attributes are wrong").isEqualTo(testData);
return target;
}

Expand Down
Loading