Skip to content

Commit

Permalink
Revert datasource state when delete fails
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed Aug 15, 2023
1 parent d8718ad commit cf344ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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 cf344ac

Please sign in to comment.