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
12 changed files
with
426 additions
and
112 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
130 changes: 130 additions & 0 deletions
130
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,130 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.spark.cluster; | ||
|
||
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.datasources.exceptions.DataSourceNotFoundException; | ||
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."); | ||
Map<String, FlintIndexMetadata> autoRefreshFlintIndicesMap = getAllAutoRefreshIndices(); | ||
autoRefreshFlintIndicesMap.forEach( | ||
(autoRefreshIndex, flintIndexMetadata) -> { | ||
try { | ||
String datasourceName = getDataSourceName(flintIndexMetadata); | ||
try { | ||
DataSourceMetadata dataSourceMetadata = | ||
this.dataSourceService.getDataSourceMetadata(datasourceName); | ||
if (dataSourceMetadata.getStatus() == DataSourceStatus.DISABLED) { | ||
alterAutoRefreshIndex(autoRefreshIndex, flintIndexMetadata, datasourceName); | ||
} | ||
} catch (DataSourceNotFoundException exception) { | ||
// Datasource disabled. | ||
vacuumAutoRefreshIndex(autoRefreshIndex, flintIndexMetadata, datasourceName); | ||
} | ||
} 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 void vacuumAutoRefreshIndex( | ||
String autoRefreshIndex, FlintIndexMetadata flintIndexMetadata, String datasourceName) { | ||
// When the datasource is deleted. Possibly Replace with VACUUM 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); | ||
} | ||
|
||
private void alterAutoRefreshIndex( | ||
String autoRefreshIndex, FlintIndexMetadata flintIndexMetadata, String 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); | ||
} | ||
|
||
private String getDataSourceName(FlintIndexMetadata flintIndexMetadata) { | ||
String kind = flintIndexMetadata.getKind(); | ||
switch (kind) { | ||
case "mv": | ||
return flintIndexMetadata.getName().split("\\.")[0]; | ||
case "skipping": | ||
case "covering": | ||
return flintIndexMetadata.getSource().split("\\.")[0]; | ||
default: | ||
throw new IllegalArgumentException("Unknown flint index kind."); | ||
} | ||
} | ||
|
||
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)); | ||
} | ||
} |
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
Oops, something went wrong.