Skip to content

feat: Add SpeculativeExecutionScheduling new algorithm with tests #5817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
* [SlackTimeScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java)
* [SpeculativeExecutionScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SpeculativeExecutionScheduling.java)
* [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
* searches
* [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/BinarySearch.java)
Expand Down Expand Up @@ -597,6 +598,7 @@
* [UnionFind](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UnionFind.java)
* [UpperBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/UpperBound.java)
* slidingwindow
* [LongestSubarrayWithSumLessOrEqualToK](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToK.java)
* [LongestSubstringWithoutRepeatingCharacters](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharacters.java)
* [MaxSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarray.java)
* [MinSumKSizeSubarray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarray.java)
Expand Down Expand Up @@ -1193,6 +1195,7 @@
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
* [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
* [SpeculativeExecutionSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SpeculativeExecutionSchedulingTest.java)
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
* searches
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
Expand Down Expand Up @@ -1228,6 +1231,7 @@
* [UnionFindTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UnionFindTest.java)
* [UpperBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/UpperBoundTest.java)
* slidingwindow
* [LongestSubarrayWithSumLessOrEqualToKTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubarrayWithSumLessOrEqualToKTest.java)
* [LongestSubstringWithoutRepeatingCharactersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/LongestSubstringWithoutRepeatingCharactersTest.java)
* [MaxSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MaxSumKSizeSubarrayTest.java)
* [MinSumKSizeSubarrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/slidingwindow/MinSumKSizeSubarrayTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.thealgorithms.scheduling;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* SpeculativeExecutionScheduling runs multiple copies of the same task in parallel,
* picking the first one that finishes successfully. It is used to mitigate the
* impact of stragglers (slow tasks).
*
* Use Case: Big data systems like Hadoop and Spark, where it helps improve job completion time.
*
* @author Hardvan
*/
public final class SpeculativeExecutionScheduling {

static class Task {
String name;
boolean completed;
long startTime;

Task(String name) {
this.name = name;
this.completed = false;
this.startTime = -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the time when the task arrives to the system and becomes available for execution.

}

void start() {
this.startTime = System.currentTimeMillis();
}

void complete() {
this.completed = true;
}

boolean hasStarted() {
return this.startTime != -1;
}
}

private final Map<String, List<Task>> taskGroups;

public SpeculativeExecutionScheduling() {
taskGroups = new HashMap<>();
}

/**
* Adds a task to the specified group.
*
* @param groupName the name of the group
* @param taskName the name of the task
*/
public void addTask(String groupName, String taskName) {
List<Task> tasks = taskGroups.computeIfAbsent(groupName, k -> new ArrayList<>());
tasks.add(new Task(taskName));
}

/**
* Executes the tasks in the specified group by assigning a start time.
*
* @param groupName the name of the group
* @return the name of the task that completed successfully
*/
public String executeTasks(String groupName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide reference explanation of this algorithm

List<Task> tasks = taskGroups.get(groupName);
if (tasks == null) {
return null;
}
for (Task task : tasks) {
if (!task.completed) {
if (!task.hasStarted()) {
task.start();
}
task.complete();
return task.name + " started at " + task.startTime;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.thealgorithms.scheduling;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SpeculativeExecutionSchedulingTest {

private SpeculativeExecutionScheduling scheduler;

@BeforeEach
public void setup() {
scheduler = new SpeculativeExecutionScheduling();
}

@Test
public void testAddAndExecuteTask() {
scheduler.addTask("Group1", "Task1");

String result = scheduler.executeTasks("Group1");

// Check for null before splitting
if (result != null) {
String[] parts = result.split(" started at ");

// Validate task name and ensure start time is a valid timestamp
assertEquals("Task1", parts[0]);
assertTrue(Long.parseLong(parts[1]) > 0, "Start time should be greater than 0");
} else {
// Handle the case where result is null
assertTrue(false, "The result should not be null after executing the task.");
}
}

@Test
public void testMultipleTasksInGroup() {
scheduler.addTask("Group1", "Task1");
scheduler.addTask("Group1", "Task2");

// Execute the first task
String result1 = scheduler.executeTasks("Group1");

// Check for null before splitting
if (result1 != null) {
String[] parts1 = result1.split(" started at ");
assertEquals("Task1", parts1[0]);
assertTrue(Long.parseLong(parts1[1]) > 0, "Start time should be greater than 0");
} else {
// Handle the case where result1 is null
assertTrue(false, "The result for Task1 should not be null.");
}

// Execute the second task
String result2 = scheduler.executeTasks("Group1");

// Check for null before splitting
if (result2 != null) {
String[] parts2 = result2.split(" started at ");
assertEquals("Task2", parts2[0]);
assertTrue(Long.parseLong(parts2[1]) > 0, "Start time should be greater than 0");
} else {
// Handle the case where result2 is null
assertTrue(false, "The result for Task2 should not be null.");
}
}

@Test
public void testExecuteAllTasks() {
scheduler.addTask("Group1", "Task1");
scheduler.addTask("Group1", "Task2");

scheduler.executeTasks("Group1");
scheduler.executeTasks("Group1");

// Confirm executing tasks again returns null
assertNull(scheduler.executeTasks("Group1"));
}

@Test
public void testEmptyTaskGroup() {
// Confirm executing tasks on an empty group returns null
assertNull(scheduler.executeTasks("Group2"));
}
}