Commit ab189d66 authored by Artem Dobrynin (EPAM)'s avatar Artem Dobrynin (EPAM)
Browse files

GONRG-998: [BackupService] Change tear-down time and use created-time stamp

https://jiraeu.epam.com/browse/GONRG-998

* Changed tear-down time to created time
* Changed backup tear-down logic in accordance with new timestamp
parent 95ffed6c
package org.opengroup.osdu.backup.shedulers;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.opengroup.osdu.backup.repository.BackupStampRepository;
import org.opengroup.osdu.backup.repository.ScheduleRepository;
@RequiredArgsConstructor
public class TearDownBackupScheduledTask implements Runnable {
private final ScheduleRepository scheduleRepository;
private final SchedulersProperties schedulersProperties;
private final BackupStampRepository backupStampRepository;
private final AssetManagerFactory assetManagerFactory;
......@@ -21,13 +31,45 @@ public class TearDownBackupScheduledTask implements Runnable {
@Override
public void run() {
tearDownDateTime = LocalDateTime.now();
EnumMap<Asset, List<BackupStamp>> backupStampsByCurrentDate = backupStampRepository
.findBackupStampsByTearDownBeforeCurrentDate(tearDownDateTime);
EnumMap<Asset, List<BackupStamp>> backupStampsForDelete = getBackupStampsForDelete(
backupStampRepository.listAvailableBackupStamps());
backupStampsByCurrentDate.entrySet().stream()
backupStampsForDelete.entrySet().stream()
.filter(assetListEntry -> !assetListEntry.getValue().isEmpty())
.forEach(assetListEntry -> assetManagerFactory.getAssetManager(assetListEntry.getKey())
.deleteBackups(assetListEntry.getValue()));
}
private EnumMap<Asset, List<BackupStamp>> getBackupStampsForDelete(
EnumMap<Asset, List<BackupStamp>> availableBackupStamps) {
List<BackupSchedule> schedules = scheduleRepository.listBackupSchedules();
EnumMap<Asset, List<BackupStamp>> backupStampsForDelete = new EnumMap<>(Asset.class);
availableBackupStamps.forEach((asset, backupStamps) -> {
List<BackupStamp> stampsForDelete = new ArrayList<>();
backupStamps.forEach(backupStamp -> {
Optional<BackupSchedule> schedule = schedules.stream()
.filter(backupSchedule ->
backupSchedule.getAssetContext().equals(backupStamp.getAssetContext()))
.findFirst();
if (schedule.isPresent() &&
backupStamp.getTearDownDate().isBefore(
tearDownDateTime.minus(
schedule.get().getLifetime(),
ChronoUnit.valueOf(
schedulersProperties.getTearDownTimeUnit().toString())
))
) {
stampsForDelete.add(backupStamp);
}
});
backupStampsForDelete.put(asset, stampsForDelete);
});
return backupStampsForDelete;
}
}
......@@ -162,10 +162,7 @@ public class DatastoreBackupManager implements AssetBackupManager {
Constants.CONTEXT_KIND, kind,
Constants.CONTEXT_BACKUP_PATH, path))
.backupRepository("Storage")
.tearDownDate(
LocalDateTime.now().plus(
backupSchedule.getLifetime(),
ChronoUnit.valueOf(schedulersProperties.getTearDownTimeUnit().toString())))
.tearDownDate(LocalDateTime.now())
.success(successful)
.build();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment