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

GONRG-863

Changes:
change @Log to @Slf4j
Added scheduler config in properties
Some tests
parent b95191f3
......@@ -35,14 +35,14 @@ public class BackupApi {
public ResponseEntity<BackupSchedule> submitBackupSchedule(
@RequestBody BackupSchedule backupSchedule) {
BackupSchedule savedBackupSchedule = backupService.submitBackupSchedule(backupSchedule);
return new ResponseEntity<BackupSchedule>(savedBackupSchedule, HttpStatus.OK);
return new ResponseEntity<BackupSchedule>(savedBackupSchedule, HttpStatus.CREATED);
}
@PutMapping("/schedule")
public ResponseEntity<BackupSchedule> updateBackupSchedule(
@RequestBody BackupSchedule backupSchedule) {
BackupSchedule savedBackupSchedule = backupService.updateBackupSchedule(backupSchedule);
return new ResponseEntity<BackupSchedule>(savedBackupSchedule, HttpStatus.OK);
return new ResponseEntity<BackupSchedule>(savedBackupSchedule, HttpStatus.ACCEPTED);
}
@GetMapping("/schedule")
......
package org.opengroup.osdu.backup.config;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.ServiceLocatorFactoryBean;
import org.springframework.context.annotation.Bean;
......
......@@ -16,4 +16,6 @@ public class SchedulersProperties {
private int tearDownPeriod;
private int initialDelay;
}
......@@ -17,7 +17,7 @@
package org.opengroup.osdu.backup.di;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.backup.config.property.EntitlementProperties;
import org.opengroup.osdu.core.common.entitlements.EntitlementsAPIConfig;
import org.opengroup.osdu.core.common.entitlements.EntitlementsFactory;
......@@ -25,7 +25,7 @@ import org.opengroup.osdu.core.common.entitlements.IEntitlementsFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.stereotype.Component;
@Log
@Slf4j
@RequiredArgsConstructor
@Component
public class EntitlementsClientFactory extends AbstractFactoryBean<IEntitlementsFactory> {
......
......@@ -17,8 +17,8 @@
package org.opengroup.osdu.backup.exception;
import java.util.logging.Level;
import lombok.extern.java.Log;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
@Log
@Slf4j
public class AppExceptionHandler {
@ExceptionHandler(AppException.class)
......@@ -36,14 +36,14 @@ public class AppExceptionHandler {
private ResponseEntity<Object> getErrorResponse(AppException e) {
String exceptionMsg = e.getOriginalException() != null
String exceptionMsg = Objects.nonNull(e.getOriginalException())
? e.getOriginalException().getMessage()
: e.getError().getMessage();
if (e.getError().getCode() > 499) {
log.log(Level.SEVERE, exceptionMsg, e.getOriginalException());
log.error(exceptionMsg, e.getOriginalException());
} else {
log.log(Level.WARNING, exceptionMsg, e.getOriginalException());
log.warn(exceptionMsg, e.getOriginalException());
}
return new ResponseEntity<Object>(e.getError(), HttpStatus.resolve(e.getError().getCode()));
......
......@@ -4,10 +4,9 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.model.BackupSchedule;
......@@ -17,7 +16,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Service;
@Log
@Slf4j
@Service
@RequiredArgsConstructor
public class SchedulerServiceImpl implements SchedulerService {
......@@ -43,6 +42,7 @@ public class SchedulerServiceImpl implements SchedulerService {
public void addTaskToScheduler(BackupSchedule backupSchedule) {
PeriodicTrigger periodicTrigger = new PeriodicTrigger(backupSchedule.getBackupPeriod(),
schedulersProperties.getBackupTimeUnit());
periodicTrigger.setInitialDelay(schedulersProperties.getInitialDelay());
periodicTrigger.setFixedRate(true);
BackupScheduledTask backupScheduledTask = new BackupScheduledTask(
......@@ -50,7 +50,7 @@ public class SchedulerServiceImpl implements SchedulerService {
ScheduledFuture<?> schedule = poolTaskScheduler.schedule(backupScheduledTask, periodicTrigger);
taskMap.put(backupSchedule, schedule);
log.log(Level.INFO, "Backup task:{0} added to scheduler", backupSchedule);
log.info("Backup task:{} added to scheduler", backupSchedule);
}
@Override
......@@ -59,7 +59,7 @@ public class SchedulerServiceImpl implements SchedulerService {
if (backupSchedule.isActive()) {
addTaskToScheduler(backupSchedule);
}
log.log(Level.INFO, "Backup task:{0} updated", backupSchedule);
log.info("Backup task:{} updated", backupSchedule);
}
@Override
......
package org.opengroup.osdu.backup.service.impl;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.repository.ScheduleRepository;
import org.opengroup.osdu.backup.shedulers.SchedulerService;
import org.opengroup.osdu.core.common.model.http.AppException;
@RunWith(MockitoJUnitRunner.class)
public class BackupServiceImplTest {
@Mock
private SchedulerService schedulerService;
@Mock
private ScheduleRepository scheduleRepository;
@InjectMocks
private BackupServiceImpl backupService;
private BackupSchedule schedule;
@Before
public void setUp() {
schedule = BackupSchedule.builder().active(true).assetType(Asset.DATASTORE).backupPeriod(1).lifetime(1).build();
}
@Test
public void startUpSchedulers() {
when(scheduleRepository.listBackupSchedules()).thenReturn(Collections.singletonList(schedule));
backupService.startUpSchedulers();
verify(schedulerService).addTaskToScheduler(schedule);
}
@Test
public void testSubmitSchedule() {
when(scheduleRepository.findByAssetContext(any())).thenReturn(null);
when(scheduleRepository.save(any())).thenReturn(schedule);
backupService.submitBackupSchedule(schedule);
verify(schedulerService).addTaskToScheduler(schedule);
}
@Test(expected = AppException.class)
public void testSubmitExistingSchedule() {
when(scheduleRepository.findByAssetContext(any())).thenReturn(schedule);
backupService.submitBackupSchedule(schedule);
}
@Test(expected = AppException.class)
public void updateBadRequestBackupSchedule() {
backupService.updateBackupSchedule(schedule);
}
}
\ No newline at end of file
......@@ -50,6 +50,7 @@ public class SchedulerServiceImplTest {
.build();
when(schedulersProperties.getBackupTimeUnit()).thenReturn(TimeUnit.SECONDS);
when(schedulersProperties.getInitialDelay()).thenReturn(0);
when(assetBackupManagerFactory.getAssetManager(any())).thenReturn(assetBackupManager);
schedulerService.initThreadPool();
}
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<springfox-version>2.7.0</springfox-version>
</properties>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<groupId>org.opengroup.osdu</groupId>
<artifactId>backup-service</artifactId>
<version>1.0.0</version>
<version>0.0.1</version>
<packaging>pom</packaging>
<name>backup-service</name>
<description>Backup service</description>
<modules>
<module>backup-core</module>
<module>provider/backup-gcp</module>
</modules>
<repositories>
<repository>
<id>${repo.releases.id}</id>
<url>${repo.releases.url}</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>${publish.releases.id}</id>
<url>${publish.releases.url}</url>
</repository>
<snapshotRepository>
<id>${publish.snapshots.id}</id>
<url>${publish.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
<modules>
<module>backup-core</module>
<module>provider/backup-gcp</module>
</modules>
<repositories>
<repository>
<id>${repo.releases.id}</id>
<url>${repo.releases.url}</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>${publish.releases.id}</id>
<url>${publish.releases.url}</url>
</repository>
<snapshotRepository>
<id>${publish.snapshots.id}</id>
<url>${publish.snapshots.url}</url>
</snapshotRepository>
</distributionManagement>
</project>
......@@ -23,15 +23,16 @@ import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.model.BackupStamp;
......@@ -43,7 +44,7 @@ import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Log
@Slf4j
@Component(value = "DATASTORE")
@RequiredArgsConstructor
public class DatastoreBackupManager implements AssetBackupManager {
......@@ -56,6 +57,8 @@ public class DatastoreBackupManager implements AssetBackupManager {
private final Storage storage;
private final SchedulersProperties schedulersProperties;
private Datastore datastore;
// TODO plan behaviour with long running backup process
......@@ -65,8 +68,7 @@ public class DatastoreBackupManager implements AssetBackupManager {
String namespace = backupSchedule.getAssetContext().get(Constants.CONTEXT_NAMESPACE);
String kind = backupSchedule.getAssetContext().get(Constants.CONTEXT_KIND);
log.log(Level.INFO, "Starting backup export for namespace:{0} kind:{1} export",
new Object[]{namespace, kind});
log.info("Starting backup export for namespace : {} kind : {}", namespace, kind);
try {
GoogleDatastoreAdminV1ExportEntitiesRequest context = buildExportEntitiesRequest(backupSchedule);
......@@ -84,8 +86,8 @@ public class DatastoreBackupManager implements AssetBackupManager {
Object outputUrlPrefix = exportTaskResultResponse.getMetadata().get(Constants.DATASTORE_OUTPUT_URL_PREFIX);
log.log(Level.INFO, "Export status done:{0} backup path:{1} export",
new Object[]{exportTaskResultResponse.getDone(), outputUrlPrefix});
log.info("Export status done : {} backup path : {} export : {}", exportTaskResultResponse.getDone(),
outputUrlPrefix, exportTaskResultResponse);
BackupStamp build = BackupStamp.builder()
.assetType(backupSchedule.getAssetType())
......@@ -94,12 +96,16 @@ public class DatastoreBackupManager implements AssetBackupManager {
Constants.CONTEXT_KIND, kind,
Constants.CONTEXT_BACKUP_PATH, outputUrlPrefix.toString()))
.backupRepository("Storage")
.tearDownDate(LocalDateTime.now().plusDays(backupSchedule.getLifetime())).build();
.tearDownDate(
LocalDateTime.now().plus(
backupSchedule.getLifetime(),
ChronoUnit.valueOf(schedulersProperties.getTearDownTimeUnit().toString())))
.build();
stampsRepository.submitBackupStamp(build);
// TODO define retry on fail behaviour
} catch (IOException e) {
throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e.getMessage());
log.error(e.getMessage());
}
}
......@@ -151,7 +157,7 @@ public class DatastoreBackupManager implements AssetBackupManager {
storageBatch.submit();
stampsRepository.deleteBackupStamps(backupStamps);
log.log(Level.INFO, "Deleted backups:{0}", collect);
log.info("Deleted backups:{}", collect);
}
private Datastore getDatastore() {
......@@ -168,7 +174,7 @@ public class DatastoreBackupManager implements AssetBackupManager {
credential.refreshAccessToken();
return new Datastore(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY,
return datastore = new Datastore(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY,
new HttpCredentialsAdapter(credential));
} catch (GeneralSecurityException | IOException e) {
......
......@@ -2,13 +2,13 @@ package org.opengroup.osdu.backup.provider.gcp.manager;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.springframework.stereotype.Component;
@Log
@Slf4j
@Component(value = "ELASTIC")
@RequiredArgsConstructor
public class ElasticBackupManager implements AssetBackupManager {
......
......@@ -11,4 +11,5 @@ osdu.entitlements.app-key=test
osdu.scheduler.tear-down-time-unit=hours
osdu.scheduler.tear-down-period=24
osdu.scheduler.backup-time-unit=minutes
osdu.scheduler.initial-delay=1
LOG_PREFIX=${log.prefix}
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