Commit e9fee6c3 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 a97bd09c
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;
......@@ -15,13 +19,19 @@ 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 final ScheduleRepository scheduleRepository;
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;
......@@ -50,8 +60,9 @@ public class TearDownBackupScheduledTask implements Runnable {
backupStamps.forEach(backupStamp -> {
Optional<BackupSchedule> schedule = schedules.stream()
.filter(backupSchedule ->
backupSchedule.getAssetContext().equals(backupStamp.getAssetContext()))
.filter(backupSchedule -> isValidScheduleForAssetContext(
backupSchedule, backupStamp.getAssetContext())
)
.findFirst();
if (schedule.isPresent() &&
......@@ -72,4 +83,32 @@ public class TearDownBackupScheduledTask implements Runnable {
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);
}
......
......@@ -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