Creating AutoAssignScheduler and Checker in the Configuration class

Signed-off-by: Dominik Herbst <dominik.herbst@bosch-si.com>
This commit is contained in:
Dominik Herbst
2016-10-04 14:27:41 +02:00
parent 339f45ca6a
commit 1063c91724
3 changed files with 122 additions and 54 deletions

View File

@@ -43,6 +43,8 @@ import org.eclipse.hawkbit.repository.jpa.JpaTargetManagement;
import org.eclipse.hawkbit.repository.jpa.JpaTenantConfigurationManagement; import org.eclipse.hawkbit.repository.jpa.JpaTenantConfigurationManagement;
import org.eclipse.hawkbit.repository.jpa.JpaTenantStatsManagement; import org.eclipse.hawkbit.repository.jpa.JpaTenantStatsManagement;
import org.eclipse.hawkbit.repository.jpa.aspects.ExceptionMappingAspectHandler; import org.eclipse.hawkbit.repository.jpa.aspects.ExceptionMappingAspectHandler;
import org.eclipse.hawkbit.repository.jpa.autoassign.AutoAssignChecker;
import org.eclipse.hawkbit.repository.jpa.autoassign.AutoAssignScheduler;
import org.eclipse.hawkbit.repository.jpa.configuration.MultiTenantJpaTransactionManager; import org.eclipse.hawkbit.repository.jpa.configuration.MultiTenantJpaTransactionManager;
import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder; import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder;
import org.eclipse.hawkbit.repository.jpa.model.helper.CacheManagerHolder; import org.eclipse.hawkbit.repository.jpa.model.helper.CacheManagerHolder;
@@ -390,4 +392,46 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
public EntityFactory entityFactory() { public EntityFactory entityFactory() {
return new JpaEntityFactory(); return new JpaEntityFactory();
} }
/**
* {@link AutoAssignChecker} bean.
*
* @param targetFilterQueryManagement
* to get all target filter queries
* @param targetManagement
* to get targets
* @param deploymentManagement
* to assign distribution sets to targets
* @param transactionManager
* to run transactions
* @return a new {@link AutoAssignChecker}
*/
@Bean
@ConditionalOnMissingBean
public AutoAssignChecker autoAssignChecker(TargetFilterQueryManagement targetFilterQueryManagement,
TargetManagement targetManagement, DeploymentManagement deploymentManagement,
PlatformTransactionManager transactionManager) {
return new AutoAssignChecker(targetFilterQueryManagement, targetManagement, deploymentManagement,
transactionManager);
}
/**
* {@link AutoAssignScheduler} bean.
*
* @param tenantAware
* to run as specific tenant
* @param systemManagement
* to find all tenants
* @param systemSecurityContext
* to run as system
* @param autoAssignChecker
* to run a check as tenant
* @return a new {@link AutoAssignChecker}
*/
@Bean
@ConditionalOnMissingBean
public AutoAssignScheduler autoAssignScheduler(TenantAware tenantAware, SystemManagement systemManagement,
SystemSecurityContext systemSecurityContext, AutoAssignChecker autoAssignChecker) {
return new AutoAssignScheduler(tenantAware, systemManagement, systemSecurityContext, autoAssignChecker);
}
} }

View File

@@ -23,68 +23,79 @@ import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.eclipse.hawkbit.repository.model.TargetWithActionType; import org.eclipse.hawkbit.repository.model.TargetWithActionType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
/** /**
* Checks if targets need a new distribution set (DS) based on the target filter queries and * Checks if targets need a new distribution set (DS) based on the target filter
* assigns the new DS when necessary. * queries and assigns the new DS when necessary. First all target filter
* First all target filter queries are listed. For every target filter query (TFQ) the auto assign DS * queries are listed. For every target filter query (TFQ) the auto assign DS is
* is retrieved. * retrieved. All targets get listed per target filter query, that match the TFQ
* All targets get listed per target filter query, that match the TFQ and that don't have the * and that don't have the auto assign DS in their action history.
* auto assign DS in their action history.
*/ */
@Component
public class AutoAssignChecker { public class AutoAssignChecker {
private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignChecker.class); private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignChecker.class);
@Autowired private final TargetFilterQueryManagement targetFilterQueryManagement;
private TargetFilterQueryManagement targetFilterQueryManagement;
@Autowired private final TargetManagement targetManagement;
private TargetManagement targetManagement;
@Autowired private final DeploymentManagement deploymentManagement;
private DeploymentManagement deploymentManagement;
@Autowired
private PlatformTransactionManager transactionManager;
private TransactionTemplate transactionTemplate;
private final TransactionTemplate transactionTemplate;
/** /**
* Maximum for target filter queries with auto assign DS * Maximum for target filter queries with auto assign DS Maximum for targets
* Maximum for targets that are fetched in one turn * that are fetched in one turn
*/ */
private static final int PAGE_SIZE = 1000; private static final int PAGE_SIZE = 1000;
/** /**
* The message which is added to the action status when a distribution set is assigned * The message which is added to the action status when a distribution set
* to an target. First %s is the name of the target filter. * 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"; private static final String ACTION_MESSAGE = "Auto assignment by target filter: %s";
/** /**
* Checks all target filter queries with an auto assign distribution set * Instantiates a new auto assign checker
* and triggers the check and assignment to targets that don't have the design DS yet *
* @param targetFilterQueryManagement
* to get all target filter queries
* @param targetManagement
* to get targets
* @param deploymentManagement
* to assign distribution sets to targets
* @param transactionManager
* to run transactions
*/ */
public AutoAssignChecker(TargetFilterQueryManagement targetFilterQueryManagement, TargetManagement targetManagement,
DeploymentManagement deploymentManagement, PlatformTransactionManager transactionManager) {
this.targetFilterQueryManagement = targetFilterQueryManagement;
this.targetManagement = targetManagement;
this.deploymentManagement = deploymentManagement;
final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("autoAssignDSToTargets");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionTemplate = new TransactionTemplate(transactionManager, def);
}
/**
* 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
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void check() { public void check() {
if(transactionTemplate == null) {
final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("autoAssignDSToTargets");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionTemplate = new TransactionTemplate(transactionManager, def);
}
PageRequest pageRequest = new PageRequest(0, PAGE_SIZE); PageRequest pageRequest = new PageRequest(0, PAGE_SIZE);
@@ -99,9 +110,9 @@ public class AutoAssignChecker {
/** /**
* Fetches the distribution set, gets all controllerIds and assigns the DS * Fetches the distribution set, gets all controllerIds and assigns the DS
* to them. * to them. Catches PersistenceException and own exceptions derived from
* Catches PersistenceException and own exceptions derived from AbstractServerRtException * AbstractServerRtException
* *
* @param targetFilterQuery * @param targetFilterQuery
* the target filter query * the target filter query
*/ */
@@ -124,7 +135,7 @@ public class AutoAssignChecker {
/** /**
* Runs one page of target assignments within a dedicated transaction * Runs one page of target assignments within a dedicated transaction
* *
* @param targetFilterQuery * @param targetFilterQuery
* the target filter query * the target filter query
* @param dsId * @param dsId
@@ -144,8 +155,9 @@ public class AutoAssignChecker {
} }
/** /**
* Gets all matching targets with the designated action from the target management * Gets all matching targets with the designated action from the target
* * management
*
* @param targetFilterQuery * @param targetFilterQuery
* the query the targets have to match * the query the targets have to match
* @param dsId * @param dsId
@@ -155,14 +167,13 @@ public class AutoAssignChecker {
* maximum amount of targets to retrieve * maximum amount of targets to retrieve
* @return list of targets with action type * @return list of targets with action type
*/ */
private List<TargetWithActionType> getTargetsWithActionType(TargetFilterQuery targetFilterQuery, Long dsId, int count) { private List<TargetWithActionType> getTargetsWithActionType(TargetFilterQuery targetFilterQuery, Long dsId,
int count) {
Page<Target> targets = targetManagement.findAllTargetsByTargetFilterQueryAndNonDS(new PageRequest(0, count), Page<Target> targets = targetManagement.findAllTargetsByTargetFilterQueryAndNonDS(new PageRequest(0, count),
dsId, targetFilterQuery); dsId, targetFilterQuery);
return targets.getContent().stream() return targets.getContent().stream().map(t -> new TargetWithActionType(t.getControllerId(),
.map(t -> new TargetWithActionType(t.getControllerId(), Action.ActionType.FORCED, Action.ActionType.FORCED, RepositoryModelConstants.NO_FORCE_TIME)).collect(Collectors.toList());
RepositoryModelConstants.NO_FORCE_TIME))
.collect(Collectors.toList());
} }
} }

View File

@@ -16,16 +16,13 @@ import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantAware; import org.eclipse.hawkbit.tenancy.TenantAware;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/** /**
* Scheduler to check target filters for auto assignment of distribution sets * Scheduler to check target filters for auto assignment of distribution sets
*/ */
@Component
// don't active the auto assign scheduler in test, otherwise it is hard to test // don't active the auto assign scheduler in test, otherwise it is hard to test
@Profile("!test") @Profile("!test")
@EnableConfigurationProperties(AutoAssignProperties.class) @EnableConfigurationProperties(AutoAssignProperties.class)
@@ -33,17 +30,33 @@ public class AutoAssignScheduler {
private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignScheduler.class); private static final Logger LOGGER = LoggerFactory.getLogger(AutoAssignScheduler.class);
@Autowired private final TenantAware tenantAware;
private TenantAware tenantAware;
@Autowired private final SystemManagement systemManagement;
private SystemManagement systemManagement;
@Autowired private final SystemSecurityContext systemSecurityContext;
private SystemSecurityContext systemSecurityContext;
@Autowired private final AutoAssignChecker autoAssignChecker;
private AutoAssignChecker autoAssignChecker;
/**
* Instantiates a new AutoAssignScheduler
*
* @param tenantAware
* to run as specific tenant
* @param systemManagement
* to find all tenants
* @param systemSecurityContext
* to run as system
* @param autoAssignChecker
* to run a check as tenant
*/
public AutoAssignScheduler(TenantAware tenantAware, SystemManagement systemManagement,
SystemSecurityContext systemSecurityContext, AutoAssignChecker autoAssignChecker) {
this.tenantAware = tenantAware;
this.systemManagement = systemManagement;
this.systemSecurityContext = systemSecurityContext;
this.autoAssignChecker = autoAssignChecker;
}
/** /**
* Scheduler method called by the spring-async mechanism. Retrieves all * Scheduler method called by the spring-async mechanism. Retrieves all