From cf344ac53f9ea56c61cd88ae9be0be2768f6f681 Mon Sep 17 00:00:00 2001 From: Heemin Kim Date: Tue, 15 Aug 2023 16:10:18 -0700 Subject: [PATCH] Revert datasource state when delete fails Signed-off-by: Heemin Kim --- .../action/DeleteDatasourceTransportAction.java | 13 +++++++++++-- .../DeleteDatasourceTransportActionTests.java | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportAction.java index 0522e228..cfd12ba3 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportAction.java @@ -111,9 +111,18 @@ protected void deleteDatasource(final String datasourceName) throws IOException if (datasource == null) { throw new ResourceNotFoundException("no such datasource exist"); } - + DatasourceState previousState = datasource.getState(); setDatasourceStateAsDeleting(datasource); - geoIpDataDao.deleteIp2GeoDataIndex(datasource.getIndices()); + + try { + geoIpDataDao.deleteIp2GeoDataIndex(datasource.getIndices()); + } catch (Exception e) { + if (previousState.equals(datasource.getState()) == false) { + datasource.setState(previousState); + datasourceDao.updateDatasource(datasource); + } + throw e; + } datasourceDao.deleteDatasource(datasource); } diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportActionTests.java index 3db3583a..aeb4e594 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/DeleteDatasourceTransportActionTests.java @@ -8,6 +8,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -162,4 +163,19 @@ public void testDeleteDatasource_whenProcessorIsCreatedDuringDeletion_thenThrowE verify(geoIpDataDao, never()).deleteIp2GeoDataIndex(datasource.getIndices()); verify(datasourceDao, never()).deleteDatasource(datasource); } + + @SneakyThrows + public void testDeleteDatasource_whenDeleteFailsAfterStateIsChanged_thenRevertState() { + Datasource datasource = randomDatasource(); + datasource.setState(DatasourceState.AVAILABLE); + when(datasourceDao.getDatasource(datasource.getName())).thenReturn(datasource); + doThrow(new RuntimeException()).when(geoIpDataDao).deleteIp2GeoDataIndex(datasource.getIndices()); + + // Run + expectThrows(RuntimeException.class, () -> action.deleteDatasource(datasource.getName())); + + // Verify + verify(datasourceDao, times(2)).updateDatasource(datasource); + assertEquals(DatasourceState.AVAILABLE, datasource.getState()); + } }