Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert datasource state when delete fails #382

Merged
merged 1 commit into from
Aug 16, 2023
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the scenario when datasource has previous state? We're setting it to "deleting" before we call delete method in setDatasourceStateAsDeleting().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We first change the datasource state to "deleting" and run the delete process. So, datasource always has previous state either "available", "create_failed", or "deleting".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got more info on scenario, we need to delete two resources(index and metadata), if we fail to delete metadata, we don’t revert state because index for data is already deleted

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());
}
}
Loading