Decouple auto assign check (#1000)

* Introduce auto assign executer interface to override check method.
* Add copyright header.
* Move documentation to interface. Annotate the AutoAssignExecutor as FunctionalInterface.
* Rename method to match interface name

Signed-off-by: Michael Herdt <Michael.Herdt2@bosch-si.com>
This commit is contained in:
Michael Herdt
2020-09-04 10:18:52 +02:00
committed by GitHub
parent 0e4b67895e
commit 1921fbf607
4 changed files with 46 additions and 23 deletions

View File

@@ -0,0 +1,25 @@
/**
* Copyright (c) 2020 Bosch.IO GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.autoassign;
/**
* An interface declaration which contains the check for the auto assignment
* logic.
*/
@FunctionalInterface
public interface AutoAssignExecutor {
/**
* Checks all target filter queries with an auto assign distribution set and
* triggers the check and assignment to targets that don't have the design DS
* yet
*/
void check();
}

View File

@@ -39,6 +39,7 @@ import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.TargetTagManagement;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.eclipse.hawkbit.repository.TenantStatsManagement;
import org.eclipse.hawkbit.repository.autoassign.AutoAssignExecutor;
import org.eclipse.hawkbit.repository.builder.DistributionSetBuilder;
import org.eclipse.hawkbit.repository.builder.DistributionSetTypeBuilder;
import org.eclipse.hawkbit.repository.builder.RolloutBuilder;
@@ -756,9 +757,9 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
*/
@Bean
@ConditionalOnMissingBean
AutoAssignChecker autoAssignChecker(final TargetFilterQueryManagement targetFilterQueryManagement,
final TargetManagement targetManagement, final DeploymentManagement deploymentManagement,
final PlatformTransactionManager transactionManager) {
AutoAssignExecutor autoAssignExecutor(final TargetFilterQueryManagement targetFilterQueryManagement,
final TargetManagement targetManagement, final DeploymentManagement deploymentManagement,
final PlatformTransactionManager transactionManager) {
return new AutoAssignChecker(targetFilterQueryManagement, targetManagement, deploymentManagement,
transactionManager);
}
@@ -787,10 +788,10 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
// test
@Profile("!test")
@ConditionalOnProperty(prefix = "hawkbit.autoassign.scheduler", name = "enabled", matchIfMissing = true)
AutoAssignScheduler autoAssignScheduler(final TenantAware tenantAware, final SystemManagement systemManagement,
final SystemSecurityContext systemSecurityContext, final AutoAssignChecker autoAssignChecker,
AutoAssignScheduler autoAssignScheduler(final SystemManagement systemManagement,
final SystemSecurityContext systemSecurityContext, final AutoAssignExecutor autoAssignExecutor,
final LockRegistry lockRegistry) {
return new AutoAssignScheduler(systemManagement, systemSecurityContext, autoAssignChecker, lockRegistry);
return new AutoAssignScheduler(systemManagement, systemSecurityContext, autoAssignExecutor, lockRegistry);
}
/**

View File

@@ -17,6 +17,7 @@ import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.autoassign.AutoAssignExecutor;
import org.eclipse.hawkbit.repository.jpa.utils.DeploymentHelper;
import org.eclipse.hawkbit.repository.model.Action.ActionType;
import org.eclipse.hawkbit.repository.model.DeploymentRequest;
@@ -39,7 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
* retrieved. All targets get listed per target filter query, that match the TFQ
* and that don't have the auto assign DS in their action history.
*/
public class AutoAssignChecker {
public class AutoAssignChecker implements AutoAssignExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignChecker.class);
@@ -58,8 +59,8 @@ public class AutoAssignChecker {
private static final int PAGE_SIZE = 1000;
/**
* 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.
* 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.
*/
private static final String ACTION_MESSAGE = "Auto assignment by target filter: %s";
@@ -84,11 +85,7 @@ public class AutoAssignChecker {
this.transactionManager = transactionManager;
}
/**
* Checks all target filter queries with an auto assign distribution set and
* triggers the check and assignment to targets that don't have the design
* DS yet
*/
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void check() {
LOGGER.debug("Auto assigned check call");
@@ -106,8 +103,8 @@ public class AutoAssignChecker {
}
/**
* Fetches the distribution set, gets all controllerIds and assigns the DS
* to them. Catches PersistenceException and own exceptions derived from
* Fetches the distribution set, gets all controllerIds and assigns the DS to
* them. Catches PersistenceException and own exceptions derived from
* AbstractServerRtException
*
* @param targetFilterQuery
@@ -162,8 +159,7 @@ public class AutoAssignChecker {
* @param targetFilterQuery
* the query the targets have to match
* @param dsId
* dsId the targets are not allowed to have in their action
* history
* dsId the targets are not allowed to have in their action history
* @param type
* action type for targets auto assignment
* @param count

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository.jpa.autoassign;
import java.util.concurrent.locks.Lock;
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;
@@ -29,7 +30,7 @@ public class AutoAssignScheduler {
private final SystemSecurityContext systemSecurityContext;
private final AutoAssignChecker autoAssignChecker;
private final AutoAssignExecutor autoAssignExecutor;
private final LockRegistry lockRegistry;
@@ -40,17 +41,17 @@ public class AutoAssignScheduler {
* to find all tenants
* @param systemSecurityContext
* to run as system
* @param autoAssignChecker
* @param autoAssignExecutor
* to run a check as tenant
* @param lockRegistry
* to acquire a lock per tenant
*/
public AutoAssignScheduler(final SystemManagement systemManagement,
final SystemSecurityContext systemSecurityContext, final AutoAssignChecker autoAssignChecker,
final SystemSecurityContext systemSecurityContext, final AutoAssignExecutor autoAssignExecutor,
final LockRegistry lockRegistry) {
this.systemManagement = systemManagement;
this.systemSecurityContext = systemSecurityContext;
this.autoAssignChecker = autoAssignChecker;
this.autoAssignExecutor = autoAssignExecutor;
this.lockRegistry = lockRegistry;
}
@@ -82,7 +83,7 @@ public class AutoAssignScheduler {
}
try {
systemManagement.forEachTenant(tenant -> autoAssignChecker.check());
systemManagement.forEachTenant(tenant -> autoAssignExecutor.check());
} finally {
lock.unlock();
}