Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}