From a444dd6018590167de005528f27d23ee49decabd Mon Sep 17 00:00:00 2001 From: Varun Bansal Date: Tue, 7 May 2024 10:28:01 +0530 Subject: [PATCH] [Offline Nodes] Adds new library offline tasks --- libs/offline-tasks/build.gradle | 25 ++++++ .../org/opensearch/offline_tasks/Task.java | 43 +++++++++ .../opensearch/offline_tasks/TaskClient.java | 88 +++++++++++++++++++ .../org/opensearch/offline_tasks/TaskId.java | 24 +++++ .../opensearch/offline_tasks/TaskParams.java | 15 ++++ .../opensearch/offline_tasks/TaskType.java | 24 +++++ .../offline_tasks/package-info.java | 12 +++ 7 files changed, 231 insertions(+) create mode 100644 libs/offline-tasks/build.gradle create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/Task.java create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskClient.java create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskId.java create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskParams.java create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskType.java create mode 100644 libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/package-info.java diff --git a/libs/offline-tasks/build.gradle b/libs/offline-tasks/build.gradle new file mode 100644 index 0000000000000..67c8bf0e682c0 --- /dev/null +++ b/libs/offline-tasks/build.gradle @@ -0,0 +1,25 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +dependencies { + api project(':libs:opensearch-common') + + testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" + testImplementation "junit:junit:${versions.junit}" + testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" + testImplementation(project(":test:framework")) { + exclude group: 'org.opensearch', module: 'opensearch-offline-tasks' + } +} + +tasks.named('forbiddenApisMain').configure { + replaceSignatureFiles 'jdk-signatures' +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/Task.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/Task.java new file mode 100644 index 0000000000000..68c7363e97290 --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/Task.java @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.offline_tasks; + +/** + * A Background Task to be run on Offline Node. + */ +public class Task { + + /** + * Task identifier used to uniquely identify a Task + */ + private TaskId taskId; + + /** + * Various params to used for Task execution + */ + private TaskParams params; + + /** + * Type/Category of the Task + */ + private TaskType taskType; + + /** + * Constructor for Task + * + * @param taskId Task identifier + * @param params Task Params + * @param taskType Task Type + */ + public Task(TaskId taskId, TaskParams params, TaskType taskType) { + this.taskId = taskId; + this.params = params; + this.taskType = taskType; + } +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskClient.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskClient.java new file mode 100644 index 0000000000000..6d228f01dfbf6 --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskClient.java @@ -0,0 +1,88 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.offline_tasks; + +import org.opensearch.common.annotation.ExperimentalApi; + +import java.util.List; + +/** + * Client used to interact with Task Store/Queue + * + * @opensearch.experimental + */ +@ExperimentalApi +public interface TaskClient { + + /** + * Submit a new task to Task Store/Queue + * + * @param task + */ + void submitTask(Task task); + + /** + * Claim task from Task Store/Queue. This ensure no 2 nodes work on the same task. + * + * @param taskId + */ + void claimTask(TaskId taskId); + + /** + * Get task from Task Store/Queue + * + * @param taskId + * @return + */ + Task getTask(TaskId taskId); + + /** + * Update task in Task Store/Queue + * + * @param task + * @return + */ + Task updateTask(Task task); + + /** + * Mark task as cancelled + * + * @param taskId + */ + void cancelTask(TaskId taskId); + + /** + * List all unassigned tasks + * + * @return + */ + List getUnassignedTasks(); + + /** + * List all active tasks + * + * @return + */ + List getActiveTasks(); + + /** + * List all completed tasks + * + * @return + */ + List getCompletedTasks(); + + /** + * Sends task heart beat to Task Store/Queue + * + * @param taskId + * @param timestamp + */ + void sendTaskHeartbeat(TaskId taskId, long timestamp); +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskId.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskId.java new file mode 100644 index 0000000000000..06f913a5ff67a --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskId.java @@ -0,0 +1,24 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.offline_tasks; + +/** + * Class encapsulating Task id + */ +public class TaskId { + + /** + * Id of the Task + */ + String id; + + public TaskId(String id) { + this.id = id; + } +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskParams.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskParams.java new file mode 100644 index 0000000000000..e33712f0a8377 --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskParams.java @@ -0,0 +1,15 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.offline_tasks; + +/** + * Base class for all TaskParams implementation of various TaskTypes + */ +public abstract class TaskParams { +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskType.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskType.java new file mode 100644 index 0000000000000..8bbdfcbc0e42e --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/TaskType.java @@ -0,0 +1,24 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.offline_tasks; + +/** + * Enum for task type + */ +public enum TaskType { + /** + * For all segment merge related tasks + */ + MERGE, + + /** + * For all snapshot related tasks + */ + SNAPSHOT +} diff --git a/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/package-info.java b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/package-info.java new file mode 100644 index 0000000000000..d27d75762478d --- /dev/null +++ b/libs/offline-tasks/src/main/java/org/opensearch/offline_tasks/package-info.java @@ -0,0 +1,12 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/** + * Contains telemetry related classes + */ +package org.opensearch.offline_tasks;