forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FlintStreamingJobCleanerTask Implementation
Signed-off-by: Vamsi Manohar <[email protected]>
- Loading branch information
Showing
5 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
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
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
108 changes: 108 additions & 0 deletions
108
spark/src/main/java/org/opensearch/sql/spark/cluster/FlintStreamingJobCleanerTask.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,108 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.cluster; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.sql.datasource.DataSourceService; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceStatus; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.legacy.metrics.MetricName; | ||
import org.opensearch.sql.legacy.metrics.Metrics; | ||
import org.opensearch.sql.spark.client.EMRServerlessClientFactory; | ||
import org.opensearch.sql.spark.dispatcher.model.FlintIndexOptions; | ||
import org.opensearch.sql.spark.execution.statestore.StateStore; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadata; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadataService; | ||
import org.opensearch.sql.spark.flint.operation.FlintIndexOpAlter; | ||
|
||
/** Cleaner task which alters the active streaming jobs of a disabled datasource. */ | ||
@RequiredArgsConstructor | ||
public class FlintStreamingJobCleanerTask implements Runnable { | ||
|
||
private final DataSourceService dataSourceService; | ||
private final FlintIndexMetadataService flintIndexMetadataService; | ||
private final StateStore stateStore; | ||
private final EMRServerlessClientFactory emrServerlessClientFactory; | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(FlintStreamingJobCleanerTask.class); | ||
protected static final AtomicBoolean isRunning = new AtomicBoolean(false); | ||
|
||
@Override | ||
public void run() { | ||
if (!isRunning.compareAndSet(false, true)) { | ||
LOGGER.info("Previous task is still running. Skipping this execution."); | ||
return; | ||
} | ||
try { | ||
LOGGER.info("Starting the cleaner task for disabled data sources."); | ||
List<DataSourceMetadata> s3GlueDisabledDataSources = getS3GlueDisabledDataSources(); | ||
LOGGER.info("Found {} disabled data sources to process.", s3GlueDisabledDataSources.size()); | ||
for (DataSourceMetadata dataSourceMetadata : s3GlueDisabledDataSources) { | ||
LOGGER.info("Processing disabled data source: {}", dataSourceMetadata.getName()); | ||
Map<String, FlintIndexMetadata> autoRefreshFlintIndicesMap = | ||
getAutoRefreshIndicesOfDataSource(dataSourceMetadata); | ||
LOGGER.info( | ||
"Found {} auto-refresh indices to alter for data source: {}", | ||
autoRefreshFlintIndicesMap.size(), | ||
dataSourceMetadata.getName()); | ||
autoRefreshFlintIndicesMap.forEach( | ||
(autoRefreshIndex, flintIndexMetadata) -> { | ||
try { | ||
LOGGER.debug("Attempting to alter index: {}", autoRefreshIndex); | ||
FlintIndexOptions flintIndexOptions = new FlintIndexOptions(); | ||
flintIndexOptions.setOption(FlintIndexOptions.AUTO_REFRESH, "false"); | ||
FlintIndexOpAlter flintIndexOpAlter = | ||
new FlintIndexOpAlter( | ||
flintIndexOptions, | ||
stateStore, | ||
dataSourceMetadata.getName(), | ||
emrServerlessClientFactory.getClient(), | ||
flintIndexMetadataService); | ||
flintIndexOpAlter.apply(flintIndexMetadata); | ||
LOGGER.info("Successfully altered index: {}", autoRefreshIndex); | ||
} catch (Exception exception) { | ||
LOGGER.error( | ||
"Failed to alter index {}: {}", | ||
autoRefreshIndex, | ||
exception.getMessage(), | ||
exception); | ||
Metrics.getInstance() | ||
.getNumericalMetric(MetricName.STREAMING_JOB_CLEANER_TASK_FAILURE_COUNT) | ||
.increment(); | ||
} | ||
}); | ||
} | ||
} catch (Throwable error) { | ||
LOGGER.info("Error while running the streaming job cleaner task: {}", error.getMessage()); | ||
} finally { | ||
isRunning.set(false); | ||
} | ||
} | ||
|
||
private Map<String, FlintIndexMetadata> getAutoRefreshIndicesOfDataSource( | ||
DataSourceMetadata dataSourceMetadata) { | ||
Map<String, FlintIndexMetadata> flintIndexMetadataHashMap = | ||
flintIndexMetadataService.getFlintIndexMetadata( | ||
"flint_" + dataSourceMetadata.getName() + "_*"); | ||
return flintIndexMetadataHashMap.entrySet().stream() | ||
.filter(entry -> entry.getValue().getFlintIndexOptions().autoRefresh()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private List<DataSourceMetadata> getS3GlueDisabledDataSources() { | ||
return this.dataSourceService.getDataSourceMetadata(false).stream() | ||
.filter(dataSourceMetadata -> dataSourceMetadata.getConnector() == DataSourceType.S3GLUE) | ||
.filter(dataSourceMetadata -> dataSourceMetadata.getStatus() == DataSourceStatus.DISABLED) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
spark/src/test/java/org/opensearch/sql/spark/cluster/FlintStreamingJobCleanerTaskTest.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,125 @@ | ||
package org.opensearch.sql.spark.cluster; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import static org.mockito.Mockito.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
import org.opensearch.sql.datasource.DataSourceService; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceStatus; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.legacy.metrics.MetricName; | ||
import org.opensearch.sql.legacy.metrics.Metrics; | ||
import org.opensearch.sql.spark.client.EMRServerlessClientFactory; | ||
import org.opensearch.sql.spark.execution.statestore.StateStore; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadata; | ||
import org.opensearch.sql.spark.flint.FlintIndexMetadataService; | ||
|
||
public class FlintStreamingJobCleanerTaskTest { | ||
|
||
@Mock | ||
private DataSourceService dataSourceService; | ||
@Mock | ||
private FlintIndexMetadataService flintIndexMetadataService; | ||
@Mock | ||
private StateStore stateStore; | ||
@Mock | ||
private EMRServerlessClientFactory emrServerlessClientFactory; | ||
@Mock | ||
@InjectMocks | ||
private FlintStreamingJobCleanerTask task; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
FlintStreamingJobCleanerTask.isRunning.set(false); | ||
when(Metrics.getInstance()).thenReturn(metrics); | ||
when(metrics.getNumericalMetric(MetricName.STREAMING_JOB_CLEANER_TASK_FAILURE_COUNT)).thenReturn(numericalMetric); | ||
} | ||
|
||
@Test | ||
void shouldSkipExecutionIfPreviousTaskIsRunning() { | ||
// Arrange | ||
FlintStreamingJobCleanerTask.isRunning.set(true); | ||
|
||
// Act | ||
task.run(); | ||
|
||
// Assert | ||
verifyNoInteractions(dataSourceService); | ||
} | ||
|
||
@Test | ||
void shouldProcessDisabledDataSources() { | ||
// Arrange | ||
List<DataSourceMetadata> disabledDataSources = new ArrayList<>(); | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata("testDataSource", DataSourceType.S3GLUE, "", DataSourceStatus.DISABLED); | ||
disabledDataSources.add(dataSourceMetadata); | ||
when(dataSourceService.getDataSourceMetadata(false)).thenReturn(disabledDataSources); | ||
when(flintIndexMetadataService.getFlintIndexMetadata(anyString())).thenReturn(new HashMap<>()); | ||
|
||
// Act | ||
task.run(); | ||
|
||
// Assert | ||
verify(dataSourceService).getDataSourceMetadata(false); | ||
verify(flintIndexMetadataService).getFlintIndexMetadata(contains("flint_testDataSource_*")); | ||
} | ||
|
||
@Test | ||
void shouldAttemptToAlterAutoRefreshIndices() { | ||
// Arrange | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata("testDataSource", DataSourceType.S3GLUE, "", DataSourceStatus.DISABLED); | ||
FlintIndexMetadata flintIndexMetadata = new FlintIndexMetadata("indexName", new FlintIndexOptions()); | ||
Map<String, FlintIndexMetadata> indicesMap = new HashMap<>(); | ||
indicesMap.put("indexName", flintIndexMetadata); | ||
|
||
when(dataSourceService.getDataSourceMetadata(false)).thenReturn(List.of(dataSourceMetadata)); | ||
when(flintIndexMetadataService.getFlintIndexMetadata(anyString())).thenReturn(indicesMap); | ||
|
||
// Act | ||
task.run(); | ||
|
||
// Assert | ||
verify(flintIndexMetadataService).getFlintIndexMetadata(contains("flint_testDataSource_*")); | ||
// Verify if alter operation was attempted, this could involve checking calls to a method responsible for altering indices. | ||
} | ||
|
||
@Test | ||
void shouldHandleExceptionsDuringIndexAlterationGracefully() { | ||
// Arrange | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata("testDataSource", DataSourceType.S3GLUE, "", DataSourceStatus.DISABLED); | ||
when(dataSourceService.getDataSourceMetadata(false)).thenReturn(List.of(dataSourceMetadata)); | ||
when(flintIndexMetadataService.getFlintIndexMetadata(anyString())).thenThrow(RuntimeException.class); | ||
|
||
// Act | ||
task.run(); | ||
|
||
// Assert | ||
verify(numericalMetric).increment(); | ||
// Further assertions might include checking for specific log messages. | ||
} | ||
|
||
@Test | ||
void shouldIncrementFailureMetricsWhenExceptionOccurs() { | ||
// Arrange | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata("testDataSource", DataSourceType.S3GLUE, "", DataSourceStatus.DISABLED); | ||
FlintIndexMetadata flintIndexMetadata = new FlintIndexMetadata("indexName", new FlintIndexOptions()); | ||
Map<String, FlintIndexMetadata> indicesMap = new HashMap<>(); | ||
indicesMap.put("indexName", flintIndexMetadata); | ||
|
||
when(dataSourceService.getDataSourceMetadata(false)).thenReturn(List.of(dataSourceMetadata)); | ||
when(flintIndexMetadataService.getFlintIndexMetadata(anyString())).thenReturn(indicesMap); | ||
doThrow(RuntimeException.class).when(flintIndexMetadataService).getFlintIndexMetadata(contains("flint_testDataSource_*")); | ||
|
||
// Act | ||
task.run(); | ||
|
||
// Assert | ||
verify(numericalMetric, atLeastOnce()).increment(); | ||
} | ||
} |