-
Notifications
You must be signed in to change notification settings - Fork 20.5k
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
Closed
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
decd597
feat: Add `SpeculativeExecutionScheduling` new algorithm with Junit t…
Hardvan aae156b
Update directory
Hardvan 7ddbe07
Fix
Hardvan fe199d4
Merge remote-tracking branch 'origin/speculative_new_algo' into specu…
Hardvan 7777f53
Fix
Hardvan 0ac38e2
Fix
Hardvan 52f5871
Merge branch 'master' into speculative_new_algo
Hardvan 2ecbfba
Update directory
Hardvan 33c7c02
Fix
Hardvan f24ca6c
Merge remote-tracking branch 'origin/speculative_new_algo' into specu…
Hardvan 5426626
Fix
Hardvan 9139019
Fix
Hardvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
src/main/java/com/thealgorithms/scheduling/SpeculativeExecutionScheduling.java
This file contains hidden or 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,69 @@ | ||
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; | ||
|
||
Task(String name) { | ||
this.name = name; | ||
this.completed = false; | ||
} | ||
|
||
void complete() { | ||
this.completed = true; | ||
} | ||
} | ||
|
||
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. | ||
* | ||
* @param groupName the name of the group | ||
* @return the name of the task that completed successfully | ||
*/ | ||
public String executeTasks(String groupName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
task.complete(); | ||
return task.name; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/com/thealgorithms/scheduling/SpeculativeExecutionSchedulingTest.java
This file contains hidden or 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,45 @@ | ||
package com.thealgorithms.scheduling; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
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"); | ||
assertEquals("Task1", scheduler.executeTasks("Group1")); | ||
} | ||
|
||
@Test | ||
public void testMultipleTasksInGroup() { | ||
scheduler.addTask("Group1", "Task1"); | ||
scheduler.addTask("Group1", "Task2"); | ||
assertEquals("Task1", scheduler.executeTasks("Group1")); | ||
assertEquals("Task2", scheduler.executeTasks("Group1")); | ||
} | ||
|
||
@Test | ||
public void testExecuteAllTasks() { | ||
scheduler.addTask("Group1", "Task1"); | ||
scheduler.addTask("Group1", "Task2"); | ||
scheduler.executeTasks("Group1"); | ||
scheduler.executeTasks("Group1"); | ||
assertNull(scheduler.executeTasks("Group1")); | ||
} | ||
|
||
@Test | ||
public void testEmptyTaskGroup() { | ||
assertNull(scheduler.executeTasks("Group2")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.