Commit 4e9adad2 authored by Rustam Lotsmanenko (EPAM)'s avatar Rustam Lotsmanenko (EPAM)
Browse files

Merge remote-tracking branch...

Merge remote-tracking branch 'origin/feature/GONRG-998_-_BackupService_Change_tear-down_time_and_use_created-time_stamp' into integration-master
parents fa909b4c e9fee6c3
package org.opengroup.osdu.backup.shedulers;
import static java.util.Objects.isNull;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@RequiredArgsConstructor
public class TearDownBackupScheduledTask implements Runnable {
private static final String CONTEXT_NAMESPACE = "namespace";
private static final String CONTEXT_KIND = "kind";
private static final String CONTEXT_INSTANCE = "instance";
private static final String ERROR_NOT_VALID_CONTEXT = "Cannot find valid context for schedule: {} and asset context: {}";
private final SchedulersProperties schedulersProperties;
private final ScheduleRepository scheduleRepository;
private final BackupStampRepository backupStampRepository;
private final AssetManagerFactory assetManagerFactory;
......@@ -21,13 +41,74 @@ 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 -> isValidScheduleForAssetContext(
backupSchedule, 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;
}
private boolean isValidScheduleForAssetContext(BackupSchedule schedule, Map<String, String> assetContext){
if (Asset.SQL.equals(schedule.getAssetType())) {
String contextInstance = assetContext.get(CONTEXT_INSTANCE);
String scheduleInstance = schedule.getAssetContext().get(CONTEXT_INSTANCE);
if (isNull(scheduleInstance)){
log.warn(ERROR_NOT_VALID_CONTEXT, schedule, assetContext);
return false;
}
return scheduleInstance.equals(contextInstance);
} else {
String contextKind = assetContext.get(CONTEXT_KIND);
String contextNamespace = assetContext.get(CONTEXT_NAMESPACE);
String scheduleKind = schedule.getAssetContext().get(CONTEXT_KIND);
String scheduleNamespace = schedule.getAssetContext().get(CONTEXT_NAMESPACE);
if (isNull(scheduleKind) || isNull(scheduleNamespace)){
log.warn(ERROR_NOT_VALID_CONTEXT, schedule, assetContext);
return false;
}
return
scheduleKind.equals(contextKind) &&
scheduleNamespace.equals(contextNamespace);
}
}
}
......@@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.repository.BackupStampRepository;
import org.opengroup.osdu.backup.repository.ScheduleRepository;
import org.opengroup.osdu.backup.shedulers.TearDownBackupScheduledTask;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
......@@ -16,6 +17,8 @@ public class TearDownScheduler {
private final SchedulersProperties schedulersProperties;
private final ScheduleRepository scheduleRepository;
private final AssetManagerFactory repoFactory;
private final BackupStampRepository stampsRepository;
......@@ -34,8 +37,8 @@ public class TearDownScheduler {
schedulersProperties.getTearDownTimeUnit());
periodicTrigger.setFixedRate(true);
TearDownBackupScheduledTask tearDownBackupScheduledTask = new TearDownBackupScheduledTask(stampsRepository,
repoFactory);
TearDownBackupScheduledTask tearDownBackupScheduledTask = new TearDownBackupScheduledTask(
schedulersProperties, scheduleRepository, stampsRepository, repoFactory);
poolTaskScheduler.schedule(tearDownBackupScheduledTask, periodicTrigger);
}
......
......@@ -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();
}
......
......@@ -142,9 +142,7 @@ public class SQLBackupManager implements AssetBackupManager {
.of(Constants.CONTEXT_INSTANCE, instance,
Constants.CONTEXT_BACKUP_ID, String.valueOf(id)))
.tearDownDate(
LocalDateTime.now().plus(
backupSchedule.getLifetime(),
ChronoUnit.valueOf(schedulersProperties.getTearDownTimeUnit().toString())))
LocalDateTime.now())
.success(true)
.build();
}
......
Markdown is supported
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