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
6 changed files
with
210 additions
and
5 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
125 changes: 125 additions & 0 deletions
125
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,125 @@ | ||
/* | ||
* 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.Set; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
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; | ||
import org.opensearch.sql.spark.flint.operation.FlintIndexOpCancel; | ||
|
||
/** 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 and deleted data sources."); | ||
List<DataSourceMetadata> s3GlueDisabledDataSources = getS3GlueDataSources(); | ||
Set<String> disabledS3DataSources = | ||
s3GlueDisabledDataSources.stream() | ||
.filter( | ||
dataSourceMetadata -> dataSourceMetadata.getStatus() == DataSourceStatus.DISABLED) | ||
.map(DataSourceMetadata::getName) | ||
.collect(Collectors.toSet()); | ||
Set<String> allS3DataSources = | ||
s3GlueDisabledDataSources.stream() | ||
.map(DataSourceMetadata::getName) | ||
.collect(Collectors.toSet()); | ||
Map<String, FlintIndexMetadata> autoRefreshFlintIndicesMap = getAllAutoRefreshIndices(); | ||
autoRefreshFlintIndicesMap.forEach( | ||
(autoRefreshIndex, flintIndexMetadata) -> { | ||
try { | ||
String datasourceName = getDataSourceName(autoRefreshIndex); | ||
if (disabledS3DataSources.contains(datasourceName)) { | ||
LOGGER.debug("Attempting to alter index: {}", autoRefreshIndex); | ||
FlintIndexOptions flintIndexOptions = new FlintIndexOptions(); | ||
flintIndexOptions.setOption(FlintIndexOptions.AUTO_REFRESH, "false"); | ||
FlintIndexOpAlter flintIndexOpAlter = | ||
new FlintIndexOpAlter( | ||
flintIndexOptions, | ||
stateStore, | ||
datasourceName, | ||
emrServerlessClientFactory.getClient(), | ||
flintIndexMetadataService); | ||
flintIndexOpAlter.apply(flintIndexMetadata); | ||
LOGGER.info("Successfully altered index: {}", autoRefreshIndex); | ||
} else if (!allS3DataSources.contains(datasourceName)) { | ||
//Possibly Replace with VACCUM Operation. | ||
LOGGER.debug("Attempting to cancel auto refresh index: {}", autoRefreshIndex); | ||
FlintIndexOpCancel flintIndexOpCancel = | ||
new FlintIndexOpCancel( | ||
stateStore, datasourceName, emrServerlessClientFactory.getClient()); | ||
flintIndexOpCancel.apply(flintIndexMetadata); | ||
LOGGER.info("Successfully cancelled index: {}", autoRefreshIndex); | ||
} | ||
} catch (Exception exception) { | ||
LOGGER.error( | ||
"Failed to alter/cancel 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 String getDataSourceName(String autoRefreshIndex) { | ||
String[] split = autoRefreshIndex.split("_"); | ||
return split.length > 1 ? split[1] : StringUtils.EMPTY; | ||
} | ||
|
||
private Map<String, FlintIndexMetadata> getAllAutoRefreshIndices() { | ||
Map<String, FlintIndexMetadata> flintIndexMetadataHashMap = | ||
flintIndexMetadataService.getFlintIndexMetadata("flint_*"); | ||
return flintIndexMetadataHashMap.entrySet().stream() | ||
.filter(entry -> entry.getValue().getFlintIndexOptions().autoRefresh()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private List<DataSourceMetadata> getS3GlueDataSources() { | ||
return this.dataSourceService.getDataSourceMetadata(false).stream() | ||
.filter(dataSourceMetadata -> dataSourceMetadata.getConnector() == DataSourceType.S3GLUE) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
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,40 @@ | ||
package org.opensearch.sql.spark.cluster; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.opensearch.sql.datasource.model.DataSourceMetadata; | ||
import org.opensearch.sql.datasource.model.DataSourceType; | ||
import org.opensearch.sql.spark.asyncquery.AsyncQueryExecutorServiceSpec; | ||
|
||
public class FlintStreamingJobCleanerTaskTest extends AsyncQueryExecutorServiceSpec { | ||
|
||
@Before | ||
@Override | ||
public void setup() { | ||
super.setup(); | ||
// Having other type just for extra datasource. | ||
DataSourceMetadata prometheusDataSource = | ||
new DataSourceMetadata.Builder() | ||
.setName("prometheus") | ||
.setConnector(DataSourceType.PROMETHEUS) | ||
.setProperties(ImmutableMap.of("prometheus.uri", "http://localhost:9080")) | ||
.build(); | ||
dataSourceService.createDataSource(prometheusDataSource); | ||
} | ||
|
||
@Test | ||
public void testStreamingJobCleanerWhenDataSourceDisabled() {} | ||
|
||
@Test | ||
public void testMultipleStreamingJobCleanerWhenDataSourceDisabled() {} | ||
|
||
@Test | ||
public void testStreamingJobClearnerWhenDataSourceIsDeleted() {} | ||
|
||
@Test | ||
public void testStreamingJobCleanerWhenDataSourceIsNeitherDisabledNorDeleted() {} | ||
|
||
@Test | ||
public void testStreamingJobClearnerWhenADatasourceIsDeletedAndAnotherIsDisabled() {} | ||
} |