Skip to content

Commit

Permalink
Refactoring/Improving source: repository 3 (slf4j) (#1603)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Feb 3, 2024
1 parent ba685ef commit 5821c25
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 217 deletions.
Expand Up @@ -20,6 +20,7 @@

import jakarta.persistence.EntityManager;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryProperties;
Expand Down Expand Up @@ -60,8 +61,6 @@
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
Expand All @@ -74,10 +73,9 @@
/**
* A Jpa implementation of {@link RolloutExecutor}
*/
@Slf4j
public class JpaRolloutExecutor implements RolloutExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(JpaRolloutExecutor.class);

/**
* Max amount of targets that are handled in one transaction.
*/
Expand Down Expand Up @@ -148,7 +146,7 @@ public JpaRolloutExecutor(final RolloutTargetGroupRepository rolloutTargetGroupR

@Override
public void execute(final Rollout rollout) {
LOGGER.debug("handle rollout {}", rollout.getId());
log.debug("handle rollout {}", rollout.getId());

switch (rollout.getStatus()) {
case CREATING:
Expand All @@ -170,13 +168,13 @@ public void execute(final Rollout rollout) {
handleStopRollout((JpaRollout) rollout);
break;
default:
LOGGER.error("Rollout in status {} not supposed to be handled!", rollout.getStatus());
log.error("Rollout in status {} not supposed to be handled!", rollout.getStatus());
break;
}
}

private void handleCreateRollout(final JpaRollout rollout) {
LOGGER.debug("handleCreateRollout called for rollout {}", rollout.getId());
log.debug("handleCreateRollout called for rollout {}", rollout.getId());

final List<RolloutGroup> rolloutGroups = rolloutGroupManagement.findByRollout(
PageRequest.of(0, quotaManagement.getMaxRolloutGroupsPerRollout(), Sort.by(Direction.ASC, "id")),
Expand Down Expand Up @@ -209,9 +207,9 @@ private void handleCreateRollout(final JpaRollout rollout) {

if (!rolloutApprovalStrategy.isApprovalNeeded(rollout)) {
rollout.setStatus(RolloutStatus.READY);
LOGGER.debug("rollout {} creation done. Switch to READY.", rollout.getId());
log.debug("rollout {} creation done. Switch to READY.", rollout.getId());
} else {
LOGGER.debug("rollout {} creation done. Switch to WAITING_FOR_APPROVAL.", rollout.getId());
log.debug("rollout {} creation done. Switch to WAITING_FOR_APPROVAL.", rollout.getId());
rollout.setStatus(RolloutStatus.WAITING_FOR_APPROVAL);
rolloutApprovalStrategy.onApprovalRequired(rollout);
}
Expand All @@ -222,13 +220,13 @@ private void handleCreateRollout(final JpaRollout rollout) {
}

private void handleDeleteRollout(final JpaRollout rollout) {
LOGGER.debug("handleDeleteRollout called for {}", rollout.getId());
log.debug("handleDeleteRollout called for {}", rollout.getId());

// check if there are actions beyond schedule
boolean hardDeleteRolloutGroups = !actionRepository.existsByRolloutIdAndStatusNotIn(rollout.getId(),
Status.SCHEDULED);
if (hardDeleteRolloutGroups) {
LOGGER.debug("Rollout {} has no actions other than scheduled -> hard delete", rollout.getId());
log.debug("Rollout {} has no actions other than scheduled -> hard delete", rollout.getId());
hardDeleteRollout(rollout);
return;
}
Expand Down Expand Up @@ -265,7 +263,7 @@ private void handleDeleteRollout(final JpaRollout rollout) {
}

private void handleStopRollout(final JpaRollout rollout) {
LOGGER.debug("handleStopRollout called for {}", rollout.getId());
log.debug("handleStopRollout called for {}", rollout.getId());
// clean up all scheduled actions
final Slice<JpaAction> scheduledActions = findScheduledActionsByRollout(rollout);
deleteScheduledActions(rollout, scheduledActions);
Expand Down Expand Up @@ -299,27 +297,27 @@ private void handleStopRollout(final JpaRollout rollout) {

private void handleReadyRollout(final Rollout rollout) {
if (rollout.getStartAt() != null && rollout.getStartAt() <= System.currentTimeMillis()) {
LOGGER.debug(
log.debug(
"handleReadyRollout called for rollout {} with autostart beyond define time. Switch to STARTING",
rollout.getId());
rolloutManagement.start(rollout.getId());
}
}

private void handleStartingRollout(final Rollout rollout) {
LOGGER.debug("handleStartingRollout called for rollout {}", rollout.getId());
log.debug("handleStartingRollout called for rollout {}", rollout.getId());

if (ensureAllGroupsAreScheduled(rollout)) {
startFirstRolloutGroup(rollout);
}
}

private void handleRunningRollout(final JpaRollout rollout) {
LOGGER.debug("handleRunningRollout called for rollout {}", rollout.getId());
log.debug("handleRunningRollout called for rollout {}", rollout.getId());

if (rollout.isDynamic()) {
if (fillDynamicRolloutGroupsWithTargets(rollout)) {
LOGGER.debug("Dynamic group created for rollout {}", rollout.getId());
log.debug("Dynamic group created for rollout {}", rollout.getId());
return;
}
}
Expand All @@ -337,12 +335,12 @@ private void handleRunningRollout(final JpaRollout rollout) {
// to find the latest group which
executeLatestRolloutGroup(rollout);
} else {
LOGGER.debug("Rollout {} has {} running groups", rollout.getId(), rolloutGroupsRunning.size());
log.debug("Rollout {} has {} running groups", rollout.getId(), rolloutGroupsRunning.size());
executeRolloutGroups(rollout, rolloutGroupsRunning, rollout.getRolloutGroups().get(rollout.getRolloutGroups().size() - 1));
}

if (isRolloutComplete(rollout)) {
LOGGER.info("Rollout {} is finished, setting FINISHED status", rollout);
log.info("Rollout {} is finished, setting FINISHED status", rollout);
rollout.setStatus(RolloutStatus.FINISHED);
rolloutRepository.save(rollout);
}
Expand All @@ -365,7 +363,7 @@ private void deleteScheduledActions(final JpaRollout rollout, final Slice<JpaAct
afterCommit.afterCommit(() -> eventPublisherHolder.getEventPublisher()
.publishEvent(new RolloutUpdatedEvent(rollout, eventPublisherHolder.getApplicationId())));
} catch (final RuntimeException e) {
LOGGER.error("Exception during deletion of actions of rollout {}", rollout, e);
log.error("Exception during deletion of actions of rollout {}", rollout, e);
}
}
}
Expand Down Expand Up @@ -456,7 +454,7 @@ private void executeRolloutGroups(final JpaRollout rollout, final List<JpaRollou
// rollout because of error?
final boolean isError = checkErrorState(rollout, evalProxy);
if (isError) {
LOGGER.info("Rollout {} {} has error, calling error action", rollout.getName(), rollout.getId());
log.info("Rollout {} {} has error, calling error action", rollout.getName(), rollout.getId());
callErrorAction(rollout, rolloutGroup);
} else {
// not in error so check finished state, do we need to
Expand Down Expand Up @@ -492,7 +490,7 @@ private void callErrorAction(final Rollout rollout, final RolloutGroup rolloutGr
try {
evaluationManager.getErrorActionEvaluator(rolloutGroup.getErrorAction()).exec(rollout, rolloutGroup);
} catch (final EvaluatorNotConfiguredException e) {
LOGGER.error("Something bad happened when accessing the error action bean {}",
log.error("Something bad happened when accessing the error action bean {}",
rolloutGroup.getErrorAction().name(), e);
}
}
Expand All @@ -518,26 +516,26 @@ private boolean checkErrorState(final Rollout rollout, final RolloutGroup rollou
return evaluationManager.getErrorConditionEvaluator(errorCondition).eval(rollout, rolloutGroup,
rolloutGroup.getErrorConditionExp());
} catch (final EvaluatorNotConfiguredException e) {
LOGGER.error("Something bad happened when accessing the error condition bean {}", errorCondition.name(), e);
log.error("Something bad happened when accessing the error condition bean {}", errorCondition.name(), e);
return false;
}
}

private boolean checkSuccessCondition(final Rollout rollout, final RolloutGroup rolloutGroup, final RolloutGroup evalProxy,
final RolloutGroupSuccessCondition successCondition) {
LOGGER.trace("Checking finish condition {} on rolloutgroup {}", successCondition, rolloutGroup);
log.trace("Checking finish condition {} on rolloutgroup {}", successCondition, rolloutGroup);
try {
final boolean isFinished = evaluationManager.getSuccessConditionEvaluator(successCondition).eval(rollout,
evalProxy, rolloutGroup.getSuccessConditionExp());
if (isFinished) {
LOGGER.debug("Rolloutgroup {} is finished, starting next group", rolloutGroup);
log.debug("Rolloutgroup {} is finished, starting next group", rolloutGroup);
executeRolloutGroupSuccessAction(rollout, rolloutGroup);
} else {
LOGGER.debug("Rolloutgroup {} is still running", rolloutGroup);
log.debug("Rolloutgroup {} is still running", rolloutGroup);
}
return isFinished;
} catch (final EvaluatorNotConfiguredException e) {
LOGGER.error("Something bad happened when accessing the finish condition or success action bean {}",
log.error("Something bad happened when accessing the finish condition or success action bean {}",
successCondition.name(), e);
return false;
}
Expand All @@ -548,7 +546,7 @@ private void executeRolloutGroupSuccessAction(final Rollout rollout, final Rollo
}

private void startFirstRolloutGroup(final Rollout rollout) {
LOGGER.debug("startFirstRolloutGroup called for rollout {}", rollout.getId());
log.debug("startFirstRolloutGroup called for rollout {}", rollout.getId());
RolloutHelper.verifyRolloutInStatus(rollout, RolloutStatus.STARTING);
final JpaRollout jpaRollout = (JpaRollout) rollout;

Expand Down Expand Up @@ -643,7 +641,7 @@ private RolloutGroup fillRolloutGroupWithTargets(final JpaRollout rollout, final
return rolloutGroupRepository.save(group);

} catch (final TransactionException e) {
LOGGER.warn("Transaction assigning Targets to RolloutGroup failed", e);
log.warn("Transaction assigning Targets to RolloutGroup failed", e);
return group;
}
}
Expand Down Expand Up @@ -730,7 +728,7 @@ private boolean fillDynamicRolloutGroupsWithTargets(final JpaRollout rollout) {
return true;
}
} catch (final TransactionException e) {
LOGGER.warn("Transaction assigning Targets to RolloutGroup failed", e);
log.warn("Transaction assigning Targets to RolloutGroup failed", e);
}
return false;
}
Expand Down Expand Up @@ -817,7 +815,7 @@ private long createActionsForRolloutGroup(final Rollout rollout, final RolloutGr
} while (actionsCreated > 0);

} catch (final TransactionException e) {
LOGGER.warn("Transaction assigning Targets to RolloutGroup failed", e);
log.warn("Transaction assigning Targets to RolloutGroup failed", e);
return 0;
}
return totalActionsCreated;
Expand Down
Expand Up @@ -12,22 +12,21 @@
import java.util.List;
import java.util.concurrent.locks.Lock;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.ContextAware;
import org.eclipse.hawkbit.repository.RolloutExecutor;
import org.eclipse.hawkbit.repository.RolloutHandler;
import org.eclipse.hawkbit.repository.RolloutManagement;
import org.eclipse.hawkbit.repository.jpa.utils.DeploymentHelper;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.transaction.PlatformTransactionManager;

/**
* JPA implementation of {@link RolloutHandler}.
*/
@Slf4j
public class JpaRolloutHandler implements RolloutHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(JpaRolloutHandler.class);

private final TenantAware tenantAware;
private final RolloutManagement rolloutManagement;
Expand Down Expand Up @@ -73,18 +72,18 @@ public void handleAll() {
final String handlerId = createRolloutLockKey(tenantAware.getCurrentTenant());
final Lock lock = lockRegistry.obtain(handlerId);
if (!lock.tryLock()) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Could not perform lock {}", lock);
if (log.isTraceEnabled()) {
log.trace("Could not perform lock {}", lock);
}
return;
}

try {
LOGGER.trace("Trigger handling {} rollouts.", rollouts.size());
log.trace("Trigger handling {} rollouts.", rollouts.size());
rollouts.forEach(rolloutId -> handleRolloutInNewTransaction(rolloutId, handlerId));
} finally {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Unlock lock {}", lock);
if (log.isTraceEnabled()) {
log.trace("Unlock lock {}", lock);
}
lock.unlock();
}
Expand Down Expand Up @@ -114,7 +113,7 @@ private void handleRolloutInNewTransaction(final long rolloutId, final String ha
})
);
},
() -> LOGGER.error("Could not retrieve rollout with id {}. Will not continue with execution.",
() -> log.error("Could not retrieve rollout with id {}. Will not continue with execution.",
rolloutId));
return 0L;
});
Expand Down
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.ContextAware;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
Expand All @@ -21,8 +22,6 @@
import org.eclipse.hawkbit.repository.model.DeploymentRequest;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
Expand All @@ -33,10 +32,9 @@
/**
* Abstract implementation of an AutoAssignExecutor
*/
@Slf4j
public abstract class AbstractAutoAssignExecutor implements AutoAssignExecutor {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAutoAssignExecutor.class);

/**
* The message which is added to the action status when a distribution set is
* assigned to an target. First %s is the name of the target filter.
Expand Down Expand Up @@ -115,10 +113,10 @@ protected void forEachFilterWithAutoAssignDS(final Consumer<TargetFilterQuery> c
})
);
} catch (final RuntimeException ex) {
LOGGER.debug(
log.debug(
"Exception on forEachFilterWithAutoAssignDS execution for tenant {} with filter id {}. Continue with next filter query.",
filterQuery.getTenant(), filterQuery.getId(), ex);
LOGGER.error(
log.error(
"Exception on forEachFilterWithAutoAssignDS execution for tenant {} with filter id {} and error message [{}]. "
+ "Continue with next filter query.",
filterQuery.getTenant(), filterQuery.getId(), ex.getMessage());
Expand Down
Expand Up @@ -11,20 +11,19 @@

import java.util.concurrent.locks.Lock;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.autoassign.AutoAssignExecutor;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Scheduler to check target filters for auto assignment of distribution sets
*/
@Slf4j
public class AutoAssignScheduler {
private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignScheduler.class);


private static final String PROP_SCHEDULER_DELAY_PLACEHOLDER = "${hawkbit.autoassign.scheduler.fixedDelay:2000}";

private final SystemManagement systemManagement;
Expand Down Expand Up @@ -83,11 +82,11 @@ private Object executeAutoAssign() {
}

try {
LOGGER.debug("Auto assign scheduled execution has acquired lock and started for each tenant.");
log.debug("Auto assign scheduled execution has acquired lock and started for each tenant.");
systemManagement.forEachTenant(tenant -> autoAssignExecutor.checkAllTargets());
} finally {
lock.unlock();
LOGGER.debug("Auto assign scheduled execution has released lock and finished.");
log.debug("Auto assign scheduled execution has released lock and finished.");
}

return null;
Expand Down

0 comments on commit 5821c25

Please sign in to comment.