Skip to content

Commit

Permalink
Revert datasource state when delete fails (#382)
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 authored Aug 16, 2023
1 parent d8718ad commit f4514c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* IP2Geo processor implementation ([#362](https://github.com/opensearch-project/geospatial/pull/362))
### Enhancements
### Bug Fixes
* Revert datasource state when delete fails([#382](https://github.com/opensearch-project/geospatial/pull/382))
### Infrastructure
* Make jacoco report to be generated faster in local ([#267](https://github.com/opensearch-project/geospatial/pull/267))
* Exclude lombok generated code from jacoco coverage report ([#268](https://github.com/opensearch-project/geospatial/pull/268))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f4514c1

Please sign in to comment.