Skip to content

Commit

Permalink
Changes in invalid input handling (#373) (#375)
Browse files Browse the repository at this point in the history
1. Switch exception type from OpenSearchException to IllegalArgumentException for invalid endpoint
2. Add cluster state validation for update API

Signed-off-by: Heemin Kim <heemin@amazon.com>
(cherry picked from commit e1f41bb)

Co-authored-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
opensearch-trigger-bot[bot] and heemin32 authored Jul 31, 2023
1 parent 7d032ef commit 7a80ae0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.geospatial.exceptions.ConcurrentModificationException;
import org.opensearch.geospatial.exceptions.IncompatibleDatasourceException;
import org.opensearch.geospatial.ip2geo.common.DatasourceManifest;
import org.opensearch.geospatial.ip2geo.common.DatasourceState;
import org.opensearch.geospatial.ip2geo.common.Ip2GeoLockService;
import org.opensearch.geospatial.ip2geo.dao.DatasourceDao;
import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource;
Expand Down Expand Up @@ -94,6 +95,11 @@ protected void doExecute(final Task task, final UpdateDatasourceRequest request,
if (datasource == null) {
throw new ResourceNotFoundException("no such datasource exist");
}
if (DatasourceState.AVAILABLE.equals(datasource.getState()) == false) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "data source is not in an [%s] state", DatasourceState.AVAILABLE)
);
}
validate(request, datasource);
updateIfChanged(request, datasource);
lockService.releaseLock(lock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
Expand Down Expand Up @@ -192,7 +193,9 @@ protected CSVParser internalGetDatabaseReader(final DatasourceManifest manifest,
}
return new CSVParser(new BufferedReader(new InputStreamReader(zipIn)), CSVFormat.RFC4180);
}
throw new OpenSearchException("database file [{}] does not exist in the zip file [{}]", manifest.getDbName(), manifest.getUrl());
throw new IllegalArgumentException(
String.format(Locale.ROOT, "database file [%s] does not exist in the zip file [%s]", manifest.getDbName(), manifest.getUrl())
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.geospatial.exceptions.IncompatibleDatasourceException;
import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase;
import org.opensearch.geospatial.ip2geo.common.DatasourceState;
import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource;
import org.opensearch.geospatial.ip2geo.jobscheduler.DatasourceTask;
import org.opensearch.jobscheduler.spi.LockModel;
Expand Down Expand Up @@ -87,6 +88,7 @@ private void validateDoExecuteWithLockError(final Exception exception) {
@SneakyThrows
public void testDoExecute_whenValidInput_thenUpdate() {
Datasource datasource = randomDatasource(Instant.now().minusSeconds(60));
datasource.setState(DatasourceState.AVAILABLE);
datasource.setTask(DatasourceTask.DELETE_UNUSED_INDICES);
Instant originalStartTime = datasource.getSchedule().getStartTime();
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
Expand Down Expand Up @@ -124,6 +126,7 @@ public void testDoExecute_whenValidInput_thenUpdate() {
@SneakyThrows
public void testDoExecute_whenNoChangesInValues_thenNoUpdate() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(datasource.getEndpoint());

Expand Down Expand Up @@ -177,9 +180,40 @@ public void testDoExecute_whenNoDatasource_thenError() {
verify(ip2GeoLockService).releaseLock(eq(lockModel));
}

@SneakyThrows
public void testDoExecute_whenNotInAvailableState_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.CREATE_FAILED);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(datasource.getEndpoint());

Task task = mock(Task.class);
when(datasourceDao.getDatasource(datasource.getName())).thenReturn(datasource);
ActionListener<AcknowledgedResponse> listener = mock(ActionListener.class);
LockModel lockModel = randomLockModel();

// Run
action.doExecute(task, request, listener);

// Verify
ArgumentCaptor<ActionListener<LockModel>> captor = ArgumentCaptor.forClass(ActionListener.class);
verify(ip2GeoLockService).acquireLock(eq(datasource.getName()), anyLong(), captor.capture());

// Run
captor.getValue().onResponse(lockModel);

// Verify
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(listener).onFailure(exceptionCaptor.capture());
assertEquals(IllegalArgumentException.class, exceptionCaptor.getValue().getClass());
exceptionCaptor.getValue().getMessage().contains("not in an available");
verify(ip2GeoLockService).releaseLock(eq(lockModel));
}

@SneakyThrows
public void testDoExecute_whenIncompatibleFields_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setEndpoint(sampleManifestUrl());

Expand Down Expand Up @@ -211,6 +245,7 @@ public void testDoExecute_whenIncompatibleFields_thenError() {
@SneakyThrows
public void testDoExecute_whenLargeUpdateInterval_thenError() {
Datasource datasource = randomDatasource();
datasource.setState(DatasourceState.AVAILABLE);
UpdateDatasourceRequest request = new UpdateDatasourceRequest(datasource.getName());
request.setUpdateInterval(TimeValue.timeValueDays(datasource.getDatabase().getValidForInDays()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testGetDatabaseReaderNoFile() throws Exception {
Instant.now().toEpochMilli(),
"tester"
);
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> noOpsGeoIpDataDao.getDatabaseReader(manifest));
Exception exception = expectThrows(IllegalArgumentException.class, () -> noOpsGeoIpDataDao.getDatabaseReader(manifest));
assertTrue(exception.getMessage().contains("does not exist"));
}

Expand Down

0 comments on commit 7a80ae0

Please sign in to comment.