-
Notifications
You must be signed in to change notification settings - Fork 141
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
9 changed files
with
429 additions
and
111 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
129 changes: 129 additions & 0 deletions
129
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,129 @@ | ||
/* | ||
* 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); | ||
// When the datasource is disabled. | ||
if (disabledS3DataSources.contains(datasourceName)) { | ||
LOGGER.info("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)) { | ||
// When the datasource is deleted. Possibly Replace with VACCUM Operation. | ||
LOGGER.info("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()); | ||
Metrics.getInstance() | ||
.getNumericalMetric(MetricName.STREAMING_JOB_CLEANER_TASK_FAILURE_COUNT) | ||
.increment(); | ||
} 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()); | ||
} | ||
} |
Oops, something went wrong.