Search in sources :

Example 1 with VersionedResource

use of com.yelp.nrtsearch.server.backup.VersionedResource in project nrtsearch by Yelp.

the class DeleteIndexBackupHandler method handle.

@Override
public DeleteIndexBackupResponse handle(IndexState indexState, DeleteIndexBackupRequest deleteIndexBackupRequest) throws HandlerException {
    DeleteIndexBackupResponse.Builder deleteIndexBackupResponseBuilder = DeleteIndexBackupResponse.newBuilder();
    String indexName = deleteIndexBackupRequest.getIndexName();
    String serviceName = deleteIndexBackupRequest.getServiceName();
    String resourceName = deleteIndexBackupRequest.getResourceName();
    String resourceData = IndexBackupUtils.getResourceData(resourceName);
    String resourceMetadata = IndexBackupUtils.getResourceMetadata(resourceName);
    String resourceVersionData = IndexBackupUtils.getResourceVersionData(resourceName);
    String resourceVersionMetadata = IndexBackupUtils.getResourceVersionMetadata(resourceName);
    int nDays = deleteIndexBackupRequest.getNDays();
    try {
        List<VersionedResource> versionedResourceData = archiver.getVersionedResource(serviceName, resourceData);
        List<VersionedResource> versionedResourceMetadata = archiver.getVersionedResource(serviceName, resourceMetadata);
        List<VersionedResource> versionedResourceVersionData = archiver.getVersionedResource(serviceName, resourceVersionData);
        List<VersionedResource> versionedResourceVersionMetadata = archiver.getVersionedResource(serviceName, resourceVersionMetadata);
        Instant now = Instant.now();
        List<String> deletedResourceDataHashes = deleteOlderThanNDays(versionedResourceData, now, nDays);
        List<String> deletedResourceMetadataHashes = deleteOlderThanNDays(versionedResourceMetadata, now, nDays);
        List<String> deletedDataVersions = deleteOlderThanNDays(versionedResourceVersionData, now, nDays);
        List<String> deletedMetadataVersions = deleteOlderThanNDays(versionedResourceVersionMetadata, now, nDays);
        return deleteIndexBackupResponseBuilder.addAllDeletedResourceDataHashes(deletedResourceDataHashes).addAllDeletedResourceMetadataHashes(deletedResourceMetadataHashes).addAllDeletedDataVersions(deletedDataVersions).addAllDeletedMetadataVersions(deletedMetadataVersions).build();
    } catch (IOException e) {
        logger.error("Error while trying to delete backup of index {} with serviceName {}, resourceName {}, nDays: {}", indexName, serviceName, resourceName, nDays, e);
        return deleteIndexBackupResponseBuilder.build();
    }
}
Also used : DeleteIndexBackupResponse(com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse) Instant(java.time.Instant) IOException(java.io.IOException) VersionedResource(com.yelp.nrtsearch.server.backup.VersionedResource)

Example 2 with VersionedResource

use of com.yelp.nrtsearch.server.backup.VersionedResource in project nrtsearch by Yelp.

the class DeleteIndexBackupHandler method deleteOlderThanNDays.

private List<String> deleteOlderThanNDays(List<VersionedResource> versionedResources, Instant now, int nDays) throws IOException {
    List<String> deletedVersions = new ArrayList<>();
    List<VersionedResource> resourcesOlderThanNDays = versionedResources.stream().filter(resourceObject -> isOlderThanNDays(resourceObject, now, nDays)).collect(Collectors.toList());
    for (VersionedResource objectToDelete : resourcesOlderThanNDays) {
        archiver.deleteVersion(objectToDelete.getServiceName(), objectToDelete.getResourceName(), objectToDelete.getVersionHash());
        deletedVersions.add(objectToDelete.getVersionHash());
    }
    return deletedVersions;
}
Also used : DeleteIndexBackupRequest(com.yelp.nrtsearch.server.grpc.DeleteIndexBackupRequest) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Logger(org.slf4j.Logger) DeleteIndexBackupResponse(com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Instant(java.time.Instant) Archiver(com.yelp.nrtsearch.server.backup.Archiver) VersionedResource(com.yelp.nrtsearch.server.backup.VersionedResource) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) VersionedResource(com.yelp.nrtsearch.server.backup.VersionedResource)

Example 3 with VersionedResource

use of com.yelp.nrtsearch.server.backup.VersionedResource in project nrtsearch by Yelp.

the class DeleteIndexBackupHandlerTest method testIsOlderThanNDays.

@Test
public void testIsOlderThanNDays() {
    Instant now = Instant.now();
    Instant date1 = now.minus(20, ChronoUnit.DAYS);
    Instant date2 = now.minus(35, ChronoUnit.DAYS);
    int nDays = 30;
    VersionedResource testObj1 = VersionedResource.builder().setCreationTimestamp(date1).createVersionedResource();
    Assert.assertEquals(false, DeleteIndexBackupHandler.isOlderThanNDays(testObj1, now, nDays));
    VersionedResource testObj2 = VersionedResource.builder().setCreationTimestamp(date2).createVersionedResource();
    Assert.assertEquals(true, DeleteIndexBackupHandler.isOlderThanNDays(testObj2, now, nDays));
}
Also used : Instant(java.time.Instant) VersionedResource(com.yelp.nrtsearch.server.backup.VersionedResource) Test(org.junit.Test)

Example 4 with VersionedResource

use of com.yelp.nrtsearch.server.backup.VersionedResource in project nrtsearch by Yelp.

the class DeleteIndexBackupHandlerTest method testDeleteIndexBackupHandlerHandle.

@Test
public void testDeleteIndexBackupHandlerHandle() throws HandlerException, IOException {
    String indexName = "testindex";
    String serviceName = "testservice";
    String resourceName = "testresource";
    int nDays = 30;
    Archiver archiver = mock(Archiver.class);
    DeleteIndexBackupHandler handler = new DeleteIndexBackupHandler(archiver);
    DeleteIndexBackupRequest request = DeleteIndexBackupRequest.newBuilder().setIndexName(indexName).setServiceName(serviceName).setResourceName(resourceName).setNDays(nDays).build();
    IndexState testIndex = mock(IndexState.class);
    Instant now = Instant.now();
    Instant date1 = now.minus(20, ChronoUnit.DAYS);
    Instant date2 = now.minus(40, ChronoUnit.DAYS);
    String versionHash1 = "hash_1";
    String versionHash2 = "hash_2";
    String resourceData = IndexBackupUtils.getResourceData(resourceName);
    String resourceMetadata = IndexBackupUtils.getResourceMetadata(resourceName);
    String resourceVersionData = IndexBackupUtils.getResourceVersionData(resourceName);
    String resourceVersionMetadata = IndexBackupUtils.getResourceVersionMetadata(resourceName);
    VersionedResource resourceData1 = buildVersionedResource(serviceName, resourceData, date1, versionHash1);
    VersionedResource resourceMetadata1 = buildVersionedResource(serviceName, resourceMetadata, date1, versionHash1);
    VersionedResource resourceVersionData1 = buildVersionedResource(serviceName, resourceVersionData, date1, versionHash1);
    VersionedResource resourceVersionMetadata1 = buildVersionedResource(serviceName, resourceVersionMetadata, date1, versionHash1);
    VersionedResource resourceData2 = buildVersionedResource(serviceName, resourceData, date2, versionHash2);
    VersionedResource resourceMetadata2 = buildVersionedResource(serviceName, resourceMetadata, date2, versionHash2);
    VersionedResource resourceVersionData2 = buildVersionedResource(serviceName, resourceVersionData, date2, versionHash2);
    VersionedResource resourceVersionMetadata2 = buildVersionedResource(serviceName, resourceVersionMetadata, date2, versionHash2);
    // data from {resource}_data folder
    when(archiver.getVersionedResource(serviceName, resourceData)).thenReturn(Arrays.asList(resourceData1, resourceData2));
    // data from {resource}_metadata folder
    when(archiver.getVersionedResource(serviceName, resourceMetadata)).thenReturn(Arrays.asList(resourceMetadata1, resourceMetadata2));
    // data from _version/{resource}_data folder
    when(archiver.getVersionedResource(serviceName, resourceVersionData)).thenReturn(Arrays.asList(resourceVersionData1, resourceVersionData2));
    // data from _version/{resource}_metadata folder
    when(archiver.getVersionedResource(serviceName, resourceVersionMetadata)).thenReturn(Arrays.asList(resourceVersionMetadata1, resourceVersionMetadata2));
    DeleteIndexBackupResponse response = handler.handle(testIndex, request);
    // verify that a 20 days old backup doesn't get deleted
    verify(archiver, never()).deleteVersion(serviceName, resourceData, versionHash1);
    verify(archiver, never()).deleteVersion(serviceName, resourceMetadata, versionHash1);
    verify(archiver, never()).deleteVersion(serviceName, resourceVersionData, versionHash1);
    verify(archiver, never()).deleteVersion(serviceName, resourceVersionMetadata, versionHash1);
    // verify that a 40 days old backup gets deleted completely
    verify(archiver).deleteVersion(serviceName, resourceData, versionHash2);
    verify(archiver).deleteVersion(serviceName, resourceMetadata, versionHash2);
    verify(archiver).deleteVersion(serviceName, resourceVersionData, versionHash2);
    verify(archiver).deleteVersion(serviceName, resourceVersionMetadata, versionHash2);
    // verify the response format: data from all four locations should be deleted. The locations:
    // {resource}_data
    Assert.assertEquals(response.getDeletedResourceDataHashesList(), Arrays.asList(versionHash2));
    // {resource}_metadata
    Assert.assertEquals(response.getDeletedResourceMetadataHashesList(), Arrays.asList(versionHash2));
    // _version/{resource}_data
    Assert.assertEquals(response.getDeletedDataVersionsList(), Arrays.asList(versionHash2));
    // _version/{resource}_metadata
    Assert.assertEquals(response.getDeletedMetadataVersionsList(), Arrays.asList(versionHash2));
}
Also used : DeleteIndexBackupRequest(com.yelp.nrtsearch.server.grpc.DeleteIndexBackupRequest) DeleteIndexBackupResponse(com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse) Instant(java.time.Instant) Archiver(com.yelp.nrtsearch.server.backup.Archiver) VersionedResource(com.yelp.nrtsearch.server.backup.VersionedResource) Test(org.junit.Test)

Aggregations

VersionedResource (com.yelp.nrtsearch.server.backup.VersionedResource)4 Instant (java.time.Instant)4 DeleteIndexBackupResponse (com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse)3 Archiver (com.yelp.nrtsearch.server.backup.Archiver)2 DeleteIndexBackupRequest (com.yelp.nrtsearch.server.grpc.DeleteIndexBackupRequest)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1