-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Abstract FlintIndex client * Fix log * Fix test function name --------- (cherry picked from commit b2403ca) Signed-off-by: Tomoyuki Morita <moritato@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f273301
commit 93588c8
Showing
8 changed files
with
282 additions
and
17 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
async-query-core/src/main/java/org/opensearch/sql/spark/flint/FlintIndexClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.flint; | ||
|
||
/** Interface to abstract access to the FlintIndex */ | ||
public interface FlintIndexClient { | ||
void deleteIndex(String indexName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...-core/src/test/java/org/opensearch/sql/spark/flint/operation/FlintIndexOpFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.flint.operation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.sql.spark.client.EMRServerlessClientFactory; | ||
import org.opensearch.sql.spark.dispatcher.model.FlintIndexOptions; | ||
import org.opensearch.sql.spark.flint.FlintIndexClient; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadataService; | ||
import org.opensearch.sql.spark.flint.FlintIndexStateModelService; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class FlintIndexOpFactoryTest { | ||
public static final String DATASOURCE_NAME = "DATASOURCE_NAME"; | ||
|
||
@Mock private FlintIndexStateModelService flintIndexStateModelService; | ||
@Mock private FlintIndexClient flintIndexClient; | ||
@Mock private FlintIndexMetadataService flintIndexMetadataService; | ||
@Mock private EMRServerlessClientFactory emrServerlessClientFactory; | ||
|
||
@InjectMocks FlintIndexOpFactory flintIndexOpFactory; | ||
|
||
@Test | ||
void getDrop() { | ||
assertNotNull(flintIndexOpFactory.getDrop(DATASOURCE_NAME)); | ||
} | ||
|
||
@Test | ||
void getAlter() { | ||
assertNotNull(flintIndexOpFactory.getAlter(new FlintIndexOptions(), DATASOURCE_NAME)); | ||
} | ||
|
||
@Test | ||
void getVacuum() { | ||
assertNotNull(flintIndexOpFactory.getDrop(DATASOURCE_NAME)); | ||
} | ||
|
||
@Test | ||
void getCancel() { | ||
assertNotNull(flintIndexOpFactory.getDrop(DATASOURCE_NAME)); | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
...y-core/src/test/java/org/opensearch/sql/spark/flint/operation/FlintIndexOpVacuumTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.flint.operation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.sql.spark.client.EMRServerlessClientFactory; | ||
import org.opensearch.sql.spark.flint.FlintIndexClient; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadata; | ||
import org.opensearch.sql.spark.flint.FlintIndexState; | ||
import org.opensearch.sql.spark.flint.FlintIndexStateModel; | ||
import org.opensearch.sql.spark.flint.FlintIndexStateModelService; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class FlintIndexOpVacuumTest { | ||
|
||
public static final String DATASOURCE_NAME = "DATASOURCE_NAME"; | ||
public static final String LATEST_ID = "LATEST_ID"; | ||
public static final String INDEX_NAME = "INDEX_NAME"; | ||
public static final FlintIndexMetadata FLINT_INDEX_METADATA_WITH_LATEST_ID = | ||
FlintIndexMetadata.builder().latestId(LATEST_ID).opensearchIndexName(INDEX_NAME).build(); | ||
public static final FlintIndexMetadata FLINT_INDEX_METADATA_WITHOUT_LATEST_ID = | ||
FlintIndexMetadata.builder().opensearchIndexName(INDEX_NAME).build(); | ||
@Mock FlintIndexClient flintIndexClient; | ||
@Mock FlintIndexStateModelService flintIndexStateModelService; | ||
@Mock EMRServerlessClientFactory emrServerlessClientFactory; | ||
@Mock FlintIndexStateModel flintIndexStateModel; | ||
@Mock FlintIndexStateModel transitionedFlintIndexStateModel; | ||
|
||
RuntimeException testException = new RuntimeException("Test Exception"); | ||
|
||
FlintIndexOpVacuum flintIndexOpVacuum; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
flintIndexOpVacuum = | ||
new FlintIndexOpVacuum( | ||
flintIndexStateModelService, | ||
DATASOURCE_NAME, | ||
flintIndexClient, | ||
emrServerlessClientFactory); | ||
} | ||
|
||
@Test | ||
public void testApplyWithEmptyLatestId() { | ||
flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITHOUT_LATEST_ID); | ||
|
||
verify(flintIndexClient).deleteIndex(INDEX_NAME); | ||
} | ||
|
||
@Test | ||
public void testApplyWithFlintIndexStateNotFound() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.empty()); | ||
|
||
assertThrows( | ||
IllegalStateException.class, | ||
() -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
} | ||
|
||
@Test | ||
public void testApplyWithNotDeletedState() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.ACTIVE); | ||
|
||
assertThrows( | ||
IllegalStateException.class, | ||
() -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
} | ||
|
||
@Test | ||
public void testApplyWithUpdateFlintIndexStateThrow() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.DELETED); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
flintIndexStateModel, FlintIndexState.VACUUMING, DATASOURCE_NAME)) | ||
.thenThrow(testException); | ||
|
||
assertThrows( | ||
IllegalStateException.class, | ||
() -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
} | ||
|
||
@Test | ||
public void testApplyWithRunOpThrow() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.DELETED); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
flintIndexStateModel, FlintIndexState.VACUUMING, DATASOURCE_NAME)) | ||
.thenReturn(transitionedFlintIndexStateModel); | ||
doThrow(testException).when(flintIndexClient).deleteIndex(INDEX_NAME); | ||
|
||
assertThrows( | ||
Exception.class, () -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
|
||
verify(flintIndexStateModelService) | ||
.updateFlintIndexState( | ||
transitionedFlintIndexStateModel, FlintIndexState.DELETED, DATASOURCE_NAME); | ||
} | ||
|
||
@Test | ||
public void testApplyWithRunOpThrowAndRollbackThrow() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.DELETED); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
flintIndexStateModel, FlintIndexState.VACUUMING, DATASOURCE_NAME)) | ||
.thenReturn(transitionedFlintIndexStateModel); | ||
doThrow(testException).when(flintIndexClient).deleteIndex(INDEX_NAME); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
transitionedFlintIndexStateModel, FlintIndexState.DELETED, DATASOURCE_NAME)) | ||
.thenThrow(testException); | ||
|
||
assertThrows( | ||
Exception.class, () -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
} | ||
|
||
@Test | ||
public void testApplyWithDeleteFlintIndexStateModelThrow() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.DELETED); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
flintIndexStateModel, FlintIndexState.VACUUMING, DATASOURCE_NAME)) | ||
.thenReturn(transitionedFlintIndexStateModel); | ||
when(flintIndexStateModelService.deleteFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenThrow(testException); | ||
|
||
assertThrows( | ||
IllegalStateException.class, | ||
() -> flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID)); | ||
} | ||
|
||
@Test | ||
public void testApplyHappyPath() { | ||
when(flintIndexStateModelService.getFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME)) | ||
.thenReturn(Optional.of(flintIndexStateModel)); | ||
when(flintIndexStateModel.getIndexState()).thenReturn(FlintIndexState.DELETED); | ||
when(flintIndexStateModelService.updateFlintIndexState( | ||
flintIndexStateModel, FlintIndexState.VACUUMING, DATASOURCE_NAME)) | ||
.thenReturn(transitionedFlintIndexStateModel); | ||
when(transitionedFlintIndexStateModel.getLatestId()).thenReturn(LATEST_ID); | ||
|
||
flintIndexOpVacuum.apply(FLINT_INDEX_METADATA_WITH_LATEST_ID); | ||
|
||
verify(flintIndexStateModelService).deleteFlintIndexStateModel(LATEST_ID, DATASOURCE_NAME); | ||
verify(flintIndexClient).deleteIndex(INDEX_NAME); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
async-query/src/main/java/org/opensearch/sql/spark/flint/OpenSearchFlintIndexClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.flint; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.opensearch.action.support.master.AcknowledgedResponse; | ||
import org.opensearch.client.Client; | ||
|
||
@RequiredArgsConstructor | ||
public class OpenSearchFlintIndexClient implements FlintIndexClient { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private final Client client; | ||
|
||
@Override | ||
public void deleteIndex(String indexName) { | ||
DeleteIndexRequest request = new DeleteIndexRequest().indices(indexName); | ||
AcknowledgedResponse response = client.admin().indices().delete(request).actionGet(); | ||
LOG.info("OpenSearch index delete result: {}", response.isAcknowledged()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.