-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): compress and index in background (#32)
- Loading branch information
Showing
7 changed files
with
186 additions
and
113 deletions.
There are no files selected for viewing
97 changes: 0 additions & 97 deletions
97
...server/src/main/java/com/chutneytesting/execution/infra/migration/ZipReportMigration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
chutney/server/src/main/java/com/chutneytesting/migration/domain/DataMigrationExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2017-2024 Enedis | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.chutneytesting.migration.domain; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class DataMigrationExecutor implements CommandLineRunner { | ||
|
||
private final List<DataMigrator> dataMigrators; | ||
private final ExecutorService executorService; | ||
|
||
|
||
public DataMigrationExecutor(List<DataMigrator> dataMigrators) { | ||
this.dataMigrators = dataMigrators; | ||
executorService = Executors.newFixedThreadPool(dataMigrators.size()); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
dataMigrators.forEach(dataMigrator -> executorService.submit(dataMigrator::migrate)); | ||
executorService.shutdown(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
chutney/server/src/main/java/com/chutneytesting/migration/domain/DataMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2017-2024 Enedis | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.chutneytesting.migration.domain; | ||
|
||
public interface DataMigrator { | ||
void migrate(); | ||
} |
73 changes: 73 additions & 0 deletions
73
...ney/server/src/main/java/com/chutneytesting/migration/domain/ExecutionReportMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2017-2024 Enedis | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.chutneytesting.migration.domain; | ||
|
||
import com.chutneytesting.execution.infra.storage.ScenarioExecutionReportJpaRepository; | ||
import com.chutneytesting.execution.infra.storage.jpa.ScenarioExecutionReportEntity; | ||
import com.chutneytesting.index.infra.ScenarioExecutionReportIndexRepository; | ||
import com.chutneytesting.migration.infra.ExecutionReportRepository; | ||
import java.util.List; | ||
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; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExecutionReportMigrator implements DataMigrator { | ||
|
||
private final ScenarioExecutionReportJpaRepository scenarioExecutionReportJpaRepository; | ||
private final ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository; | ||
private final ExecutionReportRepository executionReportRepository; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ExecutionReportMigrator.class); | ||
|
||
public ExecutionReportMigrator(ExecutionReportRepository executionReportRepository, | ||
ScenarioExecutionReportJpaRepository scenarioExecutionReportJpaRepository, ScenarioExecutionReportIndexRepository scenarioExecutionReportIndexRepository) { | ||
this.scenarioExecutionReportJpaRepository = scenarioExecutionReportJpaRepository; | ||
this.executionReportRepository = executionReportRepository; | ||
this.scenarioExecutionReportIndexRepository = scenarioExecutionReportIndexRepository; | ||
} | ||
|
||
@Override | ||
public void migrate() { | ||
if (isMigrationDone()) { | ||
LOGGER.info("Report index not empty. Skipping indexing and in-db compression..."); | ||
return; | ||
} | ||
LOGGER.info("Start indexing and in-db compression..."); | ||
PageRequest firstPage = PageRequest.of(0, 10); | ||
int count = 0; | ||
migrate(firstPage, count); | ||
} | ||
|
||
private void migrate(Pageable pageable, int previousCount) { | ||
LOGGER.debug("Indexing and compressing reports in page n° {}", pageable.getPageNumber()); | ||
Slice<ScenarioExecutionReportEntity> slice = scenarioExecutionReportJpaRepository.findAll(pageable); | ||
List<ScenarioExecutionReportEntity> reports = slice.getContent(); | ||
|
||
executionReportRepository.compressAndSaveInDb(reports); | ||
index(reports); | ||
|
||
int count = previousCount + slice.getNumberOfElements(); | ||
if (slice.hasNext()) { | ||
migrate(slice.nextPageable(), count); | ||
} else { | ||
LOGGER.info("{} report(s) successfully compressed and indexed", count); | ||
} | ||
} | ||
|
||
private void index(List<ScenarioExecutionReportEntity> reportsInDb) { | ||
scenarioExecutionReportIndexRepository.saveAll(reportsInDb); | ||
} | ||
|
||
private boolean isMigrationDone() { | ||
int indexedReports = scenarioExecutionReportIndexRepository.count(); | ||
return indexedReports > 0; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ey/server/src/main/java/com/chutneytesting/migration/infra/ExecutionReportRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2017-2024 Enedis | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.chutneytesting.migration.infra; | ||
|
||
import com.chutneytesting.execution.infra.storage.jpa.ScenarioExecutionReportEntity; | ||
import jakarta.persistence.EntityManager; | ||
import java.util.List; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
public class ExecutionReportRepository { | ||
|
||
private final EntityManager entityManager; | ||
|
||
public ExecutionReportRepository(EntityManager entityManager ) { | ||
this.entityManager = entityManager; | ||
} | ||
|
||
@Transactional | ||
public void compressAndSaveInDb(List<ScenarioExecutionReportEntity> reportsInDb) { | ||
reportsInDb.forEach(report -> { | ||
entityManager.createQuery( | ||
"UPDATE SCENARIO_EXECUTIONS_REPORTS SET report = :report WHERE id = :id") | ||
.setParameter("report", report.getReport()) | ||
.setParameter("id", report.scenarioExecutionId()) | ||
.executeUpdate(); | ||
entityManager.detach(report); | ||
}); | ||
} | ||
} |