|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.spark.scheduler; |
| 7 | + |
| 8 | +import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS; |
| 9 | + |
| 10 | +import com.google.common.annotations.VisibleForTesting; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.time.Instant; |
| 15 | +import org.apache.commons.io.IOUtils; |
| 16 | +import org.apache.logging.log4j.LogManager; |
| 17 | +import org.apache.logging.log4j.Logger; |
| 18 | +import org.opensearch.action.DocWriteRequest; |
| 19 | +import org.opensearch.action.DocWriteResponse; |
| 20 | +import org.opensearch.action.admin.indices.create.CreateIndexRequest; |
| 21 | +import org.opensearch.action.admin.indices.create.CreateIndexResponse; |
| 22 | +import org.opensearch.action.delete.DeleteRequest; |
| 23 | +import org.opensearch.action.delete.DeleteResponse; |
| 24 | +import org.opensearch.action.index.IndexRequest; |
| 25 | +import org.opensearch.action.index.IndexResponse; |
| 26 | +import org.opensearch.action.support.WriteRequest; |
| 27 | +import org.opensearch.action.update.UpdateRequest; |
| 28 | +import org.opensearch.action.update.UpdateResponse; |
| 29 | +import org.opensearch.client.Client; |
| 30 | +import org.opensearch.cluster.service.ClusterService; |
| 31 | +import org.opensearch.common.action.ActionFuture; |
| 32 | +import org.opensearch.common.xcontent.XContentType; |
| 33 | +import org.opensearch.common.xcontent.json.JsonXContent; |
| 34 | +import org.opensearch.index.engine.DocumentMissingException; |
| 35 | +import org.opensearch.index.engine.VersionConflictEngineException; |
| 36 | +import org.opensearch.jobscheduler.spi.ScheduledJobRunner; |
| 37 | +import org.opensearch.sql.spark.scheduler.job.OpenSearchRefreshIndexJob; |
| 38 | +import org.opensearch.sql.spark.scheduler.model.OpenSearchRefreshIndexJobRequest; |
| 39 | +import org.opensearch.threadpool.ThreadPool; |
| 40 | + |
| 41 | +/** Scheduler class for managing asynchronous query jobs. */ |
| 42 | +public class OpenSearchAsyncQueryScheduler { |
| 43 | + public static final String SCHEDULER_INDEX_NAME = ".async-query-scheduler"; |
| 44 | + public static final String SCHEDULER_PLUGIN_JOB_TYPE = "async-query-scheduler"; |
| 45 | + private static final String SCHEDULER_INDEX_MAPPING_FILE_NAME = |
| 46 | + "async-query-scheduler-index-mapping.yml"; |
| 47 | + private static final String SCHEDULER_INDEX_SETTINGS_FILE_NAME = |
| 48 | + "async-query-scheduler-index-settings.yml"; |
| 49 | + private static final Logger LOG = LogManager.getLogger(); |
| 50 | + |
| 51 | + private Client client; |
| 52 | + private ClusterService clusterService; |
| 53 | + |
| 54 | + /** Loads job resources, setting up required services and job runner instance. */ |
| 55 | + public void loadJobResource(Client client, ClusterService clusterService, ThreadPool threadPool) { |
| 56 | + this.client = client; |
| 57 | + this.clusterService = clusterService; |
| 58 | + OpenSearchRefreshIndexJob openSearchRefreshIndexJob = |
| 59 | + OpenSearchRefreshIndexJob.getJobRunnerInstance(); |
| 60 | + openSearchRefreshIndexJob.setClusterService(clusterService); |
| 61 | + openSearchRefreshIndexJob.setThreadPool(threadPool); |
| 62 | + openSearchRefreshIndexJob.setClient(client); |
| 63 | + } |
| 64 | + |
| 65 | + /** Schedules a new job by indexing it into the job index. */ |
| 66 | + public void scheduleJob(OpenSearchRefreshIndexJobRequest request) { |
| 67 | + if (!this.clusterService.state().routingTable().hasIndex(SCHEDULER_INDEX_NAME)) { |
| 68 | + createAsyncQuerySchedulerIndex(); |
| 69 | + } |
| 70 | + IndexRequest indexRequest = new IndexRequest(SCHEDULER_INDEX_NAME); |
| 71 | + indexRequest.id(request.getName()); |
| 72 | + indexRequest.opType(DocWriteRequest.OpType.CREATE); |
| 73 | + indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 74 | + IndexResponse indexResponse; |
| 75 | + try { |
| 76 | + indexRequest.source(request.toXContent(JsonXContent.contentBuilder(), EMPTY_PARAMS)); |
| 77 | + ActionFuture<IndexResponse> indexResponseActionFuture = client.index(indexRequest); |
| 78 | + indexResponse = indexResponseActionFuture.actionGet(); |
| 79 | + } catch (VersionConflictEngineException exception) { |
| 80 | + throw new IllegalArgumentException("A job already exists with name: " + request.getName()); |
| 81 | + } catch (Throwable e) { |
| 82 | + LOG.error("Failed to schedule job : {}", request.getName(), e); |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + |
| 86 | + if (indexResponse.getResult().equals(DocWriteResponse.Result.CREATED)) { |
| 87 | + LOG.debug("Job : {} successfully created", request.getName()); |
| 88 | + } else { |
| 89 | + throw new RuntimeException( |
| 90 | + "Schedule job failed with result : " + indexResponse.getResult().getLowercase()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** Unschedules a job by marking it as disabled and updating its last update time. */ |
| 95 | + public void unscheduleJob(String jobId) throws IOException { |
| 96 | + assertIndexExists(); |
| 97 | + OpenSearchRefreshIndexJobRequest request = |
| 98 | + OpenSearchRefreshIndexJobRequest.builder() |
| 99 | + .jobName(jobId) |
| 100 | + .enabled(false) |
| 101 | + .lastUpdateTime(Instant.now()) |
| 102 | + .build(); |
| 103 | + updateJob(request); |
| 104 | + } |
| 105 | + |
| 106 | + /** Updates an existing job with new parameters. */ |
| 107 | + public void updateJob(OpenSearchRefreshIndexJobRequest request) throws IOException { |
| 108 | + assertIndexExists(); |
| 109 | + UpdateRequest updateRequest = new UpdateRequest(SCHEDULER_INDEX_NAME, request.getName()); |
| 110 | + updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 111 | + updateRequest.doc(request.toXContent(JsonXContent.contentBuilder(), EMPTY_PARAMS)); |
| 112 | + UpdateResponse updateResponse; |
| 113 | + try { |
| 114 | + ActionFuture<UpdateResponse> updateResponseActionFuture = client.update(updateRequest); |
| 115 | + updateResponse = updateResponseActionFuture.actionGet(); |
| 116 | + } catch (DocumentMissingException exception) { |
| 117 | + throw new IllegalArgumentException("Job: " + request.getName() + " doesn't exist"); |
| 118 | + } catch (Throwable e) { |
| 119 | + LOG.error("Failed to update job : {}", request.getName(), e); |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + |
| 123 | + if (updateResponse.getResult().equals(DocWriteResponse.Result.UPDATED) |
| 124 | + || updateResponse.getResult().equals(DocWriteResponse.Result.NOOP)) { |
| 125 | + LOG.debug("Job : {} successfully updated", request.getName()); |
| 126 | + } else { |
| 127 | + throw new RuntimeException( |
| 128 | + "Update job failed with result : " + updateResponse.getResult().getLowercase()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** Removes a job by deleting its document from the index. */ |
| 133 | + public void removeJob(String jobId) { |
| 134 | + assertIndexExists(); |
| 135 | + DeleteRequest deleteRequest = new DeleteRequest(SCHEDULER_INDEX_NAME, jobId); |
| 136 | + deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 137 | + ActionFuture<DeleteResponse> deleteResponseActionFuture = client.delete(deleteRequest); |
| 138 | + DeleteResponse deleteResponse = deleteResponseActionFuture.actionGet(); |
| 139 | + |
| 140 | + if (deleteResponse.getResult().equals(DocWriteResponse.Result.DELETED)) { |
| 141 | + LOG.debug("Job : {} successfully deleted", jobId); |
| 142 | + } else if (deleteResponse.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) { |
| 143 | + throw new IllegalArgumentException("Job : " + jobId + " doesn't exist"); |
| 144 | + } else { |
| 145 | + throw new RuntimeException( |
| 146 | + "Remove job failed with result : " + deleteResponse.getResult().getLowercase()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** Creates the async query scheduler index with specified mappings and settings. */ |
| 151 | + @VisibleForTesting |
| 152 | + void createAsyncQuerySchedulerIndex() { |
| 153 | + try { |
| 154 | + InputStream mappingFileStream = |
| 155 | + OpenSearchAsyncQueryScheduler.class |
| 156 | + .getClassLoader() |
| 157 | + .getResourceAsStream(SCHEDULER_INDEX_MAPPING_FILE_NAME); |
| 158 | + InputStream settingsFileStream = |
| 159 | + OpenSearchAsyncQueryScheduler.class |
| 160 | + .getClassLoader() |
| 161 | + .getResourceAsStream(SCHEDULER_INDEX_SETTINGS_FILE_NAME); |
| 162 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest(SCHEDULER_INDEX_NAME); |
| 163 | + createIndexRequest.mapping( |
| 164 | + IOUtils.toString(mappingFileStream, StandardCharsets.UTF_8), XContentType.YAML); |
| 165 | + createIndexRequest.settings( |
| 166 | + IOUtils.toString(settingsFileStream, StandardCharsets.UTF_8), XContentType.YAML); |
| 167 | + ActionFuture<CreateIndexResponse> createIndexResponseActionFuture = |
| 168 | + client.admin().indices().create(createIndexRequest); |
| 169 | + CreateIndexResponse createIndexResponse = createIndexResponseActionFuture.actionGet(); |
| 170 | + |
| 171 | + if (createIndexResponse.isAcknowledged()) { |
| 172 | + LOG.debug("Index: {} creation Acknowledged", SCHEDULER_INDEX_NAME); |
| 173 | + } else { |
| 174 | + throw new RuntimeException("Index creation is not acknowledged."); |
| 175 | + } |
| 176 | + } catch (Throwable e) { |
| 177 | + LOG.error("Error creating index: {}", SCHEDULER_INDEX_NAME, e); |
| 178 | + throw new RuntimeException( |
| 179 | + "Internal server error while creating " |
| 180 | + + SCHEDULER_INDEX_NAME |
| 181 | + + " index: " |
| 182 | + + e.getMessage(), |
| 183 | + e); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private void assertIndexExists() { |
| 188 | + if (!this.clusterService.state().routingTable().hasIndex(SCHEDULER_INDEX_NAME)) { |
| 189 | + throw new IllegalStateException("Job index does not exist."); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** Returns the job runner instance for the scheduler. */ |
| 194 | + public static ScheduledJobRunner getJobRunner() { |
| 195 | + return OpenSearchRefreshIndexJob.getJobRunnerInstance(); |
| 196 | + } |
| 197 | +} |
0 commit comments