Skip to content

Commit

Permalink
Fix tranasaction behavior in delete tenant (#2136)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm authored Dec 10, 2024
1 parent 13fef83 commit ebb4b09
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,12 @@ public SoftwareModuleMetadata updateMetaData(final SoftwareModuleMetadataUpdate
// check if exists otherwise throw entity not found exception
final JpaSoftwareModuleMetadata metadata = (JpaSoftwareModuleMetadata) getMetaDataBySoftwareModuleId(
update.getSoftwareModuleId(), update.getKey())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleMetadata.class,
update.getSoftwareModuleId(), update.getKey()));
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleMetadata.class, update.getSoftwareModuleId(), update.getKey()));

update.getValue().ifPresent(metadata::setValue);
update.isTargetVisible().ifPresent(metadata::setTargetVisible);

JpaManagementHelper.touch(entityManager, softwareModuleRepository,
(JpaSoftwareModule) metadata.getSoftwareModule());
JpaManagementHelper.touch(entityManager, softwareModuleRepository, metadata.getSoftwareModule());
return softwareModuleMetadataRepository.save(metadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@
import org.eclipse.hawkbit.repository.report.model.SystemUsageReportWithTenants;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -140,7 +138,6 @@ public KeyGenerator currentTenantKeyGenerator() {
}

@Override
@Transactional
@Retryable(retryFor = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX,
backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public void deleteTenant(final String t) {
Expand All @@ -151,8 +148,7 @@ public void deleteTenant(final String t) {
final String tenant = t.toUpperCase();
cacheManager.evictCaches(tenant);
rolloutStatusCache.evictCaches(tenant);
tenantAware.runAsTenant(tenant, () -> {
entityManager.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, tenant);
tenantAware.runAsTenant(tenant, () -> DeploymentHelper.runInNewTransaction(txManager, "deleteTenant", status -> {
tenantMetaDataRepository.deleteByTenantIgnoreCase(tenant);
tenantConfigurationRepository.deleteByTenant(tenant);
targetRepository.deleteByTenant(tenant);
Expand All @@ -167,7 +163,7 @@ public void deleteTenant(final String t) {
artifactRepository.deleteByTenant(tenant);
softwareModuleTypeRepository.deleteByTenant(tenant);
return null;
});
}));
}

@Override
Expand Down Expand Up @@ -292,13 +288,16 @@ private void usageStatsPerTenant(final SystemUsageReportWithTenants report) {
}

private DistributionSetType createStandardSoftwareDataSetup() {
final SoftwareModuleType app = softwareModuleTypeRepository
.save(new JpaSoftwareModuleType(org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_APP_KEY,
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_APP_NAME, "Application Addons",
Integer.MAX_VALUE));
final SoftwareModuleType os = softwareModuleTypeRepository.save(new JpaSoftwareModuleType(
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_OS_KEY,
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_OS_NAME, "Core firmware or operation system", 1));
final SoftwareModuleType app = softwareModuleTypeRepository.save(
new JpaSoftwareModuleType(
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_APP_KEY,
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_APP_NAME,
"Application Addons", Integer.MAX_VALUE));
final SoftwareModuleType os = softwareModuleTypeRepository.save(
new JpaSoftwareModuleType(
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_OS_KEY,
org.eclipse.hawkbit.repository.Constants.SMT_DEFAULT_OS_NAME,
"Core firmware or operation system", 1));

// make sure the module types get their IDs
entityManager.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
* {@link Artifact} repository.
*/
@Transactional(readOnly = true)
public interface LocalArtifactRepository
extends BaseEntityRepository<JpaArtifact> {
public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifact> {

/**
* Counts artifacts size where the related software module is not
* deleted/archived.
* Counts artifacts size where the related software module is not deleted/archived.
* <p/>
* No access control applied.
*
Expand All @@ -47,17 +45,15 @@ public interface LocalArtifactRepository
Long countBySoftwareModuleDeleted(boolean deleted);

/**
* Counts current elements based on the sha1 and tenant, as well as having the
* {@link SoftwareModule} property 'deleted' with value 'false'
* Counts current elements based on the sha1 and tenant, as well as having the {@link SoftwareModule} property 'deleted' with value 'false'
* <p/>
* No access control applied
*
* @param sha1 the sha1 of the {@link Artifact}
* @param tenant the current tenant\
* @return the count of the elements
*/
long countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(
@Param("sha1") String sha1, @Param("tenant") String tenant);
long countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(@Param("sha1") String sha1, @Param("tenant") String tenant);

/**
* Searches for a {@link Artifact} based on given gridFsFileName.
Expand All @@ -84,4 +80,4 @@ long countBySha1HashAndTenantAndSoftwareModuleDeletedIsFalse(
* @return list of {@link Artifact}.
*/
Optional<Artifact> findFirstByFilenameAndSoftwareModuleId(String filename, Long softwareModuleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
import org.springframework.transaction.annotation.Transactional;

/**
* repository for operations on {@link TenantMetaData} entity.
* Repository for operations on {@link TenantMetaData} entity.
*/
@Transactional(readOnly = true)
public interface TenantMetaDataRepository
extends PagingAndSortingRepository<JpaTenantMetaData, Long>,
CrudRepository<JpaTenantMetaData, Long> {
public interface TenantMetaDataRepository extends PagingAndSortingRepository<JpaTenantMetaData, Long>, CrudRepository<JpaTenantMetaData, Long> {

/**
* Search {@link TenantMetaData} by tenant name.
Expand All @@ -47,4 +45,4 @@ public interface TenantMetaDataRepository
@Modifying
@Query("DELETE FROM JpaTenantMetaData t WHERE UPPER(t.tenant) = UPPER(:tenant)")
void deleteByTenantIgnoreCase(@Param("tenant") String tenant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
@Feature("Component Tests - Repository")
@Story("System Management")
@ExtendWith(DisposableSqlTestDatabaseExtension.class)
public class SystemManagementTest extends AbstractJpaIntegrationTest {
class SystemManagementTest extends AbstractJpaIntegrationTest {

@Test
@Description("Ensures that findTenants returns all tenants and not only restricted to the tenant which currently is logged in")
public void findTenantsReturnsAllTenantsNotOnlyWhichLoggedIn() throws Exception {
void findTenantsReturnsAllTenantsNotOnlyWhichLoggedIn() throws Exception {
assertThat(systemManagement.findTenants(PAGE).getContent()).hasSize(1);

createTestTenantsForSystemStatistics(2, 0, 0, 0);
Expand All @@ -47,7 +47,7 @@ public void findTenantsReturnsAllTenantsNotOnlyWhichLoggedIn() throws Exception

@Test
@Description("Ensures that getSystemUsageStatisticsWithTenants returns the usage of all tenants and not only the first 1000 (max page size).")
public void systemUsageReportCollectsStatisticsOfManyTenants() throws Exception {
void systemUsageReportCollectsStatisticsOfManyTenants() throws Exception {
// Prepare tenants
createTestTenantsForSystemStatistics(1050, 0, 0, 0);

Expand All @@ -57,7 +57,7 @@ public void systemUsageReportCollectsStatisticsOfManyTenants() throws Exception

@Test
@Description("Checks that the system report calculates correctly the artifact size of all tenants in the system. It ignores deleted software modules with their artifacts.")
public void systemUsageReportCollectsArtifactsOfAllTenants() throws Exception {
void systemUsageReportCollectsArtifactsOfAllTenants() throws Exception {
// Prepare tenants
createTestTenantsForSystemStatistics(2, 1234, 0, 0);

Expand All @@ -81,7 +81,7 @@ public void systemUsageReportCollectsArtifactsOfAllTenants() throws Exception {

@Test
@Description("Checks that the system report calculates correctly the targets size of all tenants in the system")
public void systemUsageReportCollectsTargetsOfAllTenants() throws Exception {
void systemUsageReportCollectsTargetsOfAllTenants() throws Exception {
// Prepare tenants
createTestTenantsForSystemStatistics(2, 0, 100, 0);

Expand All @@ -101,7 +101,7 @@ public void systemUsageReportCollectsTargetsOfAllTenants() throws Exception {

@Test
@Description("Checks that the system report calculates correctly the actions size of all tenants in the system")
public void systemUsageReportCollectsActionsOfAllTenants() throws Exception {
void systemUsageReportCollectsActionsOfAllTenants() throws Exception {
// Prepare tenants
createTestTenantsForSystemStatistics(2, 0, 20, 2);

Expand Down

0 comments on commit ebb4b09

Please sign in to comment.