Search in sources :

Example 1 with DeleteIndexBackupResponse

use of com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse 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 DeleteIndexBackupResponse

use of com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse 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)2 DeleteIndexBackupResponse (com.yelp.nrtsearch.server.grpc.DeleteIndexBackupResponse)2 Instant (java.time.Instant)2 Archiver (com.yelp.nrtsearch.server.backup.Archiver)1 DeleteIndexBackupRequest (com.yelp.nrtsearch.server.grpc.DeleteIndexBackupRequest)1 IOException (java.io.IOException)1 Test (org.junit.Test)1