Skip to content

Commit be1c0b8

Browse files
authored
Fix issue iluwatar#179: Leader Followers Pattern (iluwatar#1189)
* add leader followers pattern * use var and streams instead in App::execute * use logger instead of printing to system output stream
1 parent 6ce33ed commit be1c0b8

File tree

15 files changed

+737
-0
lines changed

15 files changed

+737
-0
lines changed

leader-followers/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: pattern
3+
title: Leader/Followers
4+
folder: leader-followers
5+
permalink: /patterns/leader-followers/
6+
categories: Concurrency
7+
tags:
8+
- Performance
9+
---
10+
11+
## Intent
12+
The Leader/Followers pattern provides a concurrency model where multiple
13+
threads can efficiently de-multiplex events and dispatch event handlers
14+
that process I/O handles shared by the threads.
15+
16+
## Class diagram
17+
![Leader/Followers class diagram](./etc/leader-followers.png)
18+
19+
## Applicability
20+
Use Leader-Followers pattern when
21+
22+
* multiple threads take turns sharing a set of event sources in order to detect, de-multiplex, dispatch and process service requests that occur on the event sources.
23+
24+
## Real world examples
25+
26+
* [ACE Thread Pool Reactor framework](https://www.dre.vanderbilt.edu/~schmidt/PDF/HPL.pdf)
27+
* [JAWS](http://www.dre.vanderbilt.edu/~schmidt/PDF/PDCP.pdf)
28+
* [Real-time CORBA](http://www.dre.vanderbilt.edu/~schmidt/PDF/RTS.pdf)
29+
30+
## Credits
31+
32+
* [Douglas C. Schmidt and Carlos O’Ryan - Leader/Followers](http://www.kircher-schwanninger.de/michael/publications/lf.pdf)
48.1 KB
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@startuml
2+
package com.iluwatar.leaderfollowers {
3+
class App {
4+
+ App()
5+
- addTasks(taskSet : TaskSet) {static}
6+
- execute(workCenter : WorkCenter, taskSet : TaskSet) {static}
7+
+ main(args : String[]) {static}
8+
}
9+
class Task {
10+
- finished : boolean
11+
- time : int
12+
+ Task(time : int)
13+
+ getTime() : int
14+
+ isFinished() : boolean
15+
+ setFinished()
16+
}
17+
class TaskHandler {
18+
+ TaskHandler()
19+
+ handleTask(task : Task)
20+
}
21+
class TaskSet {
22+
- queue : BlockingQueue<Task>
23+
+ TaskSet()
24+
+ addTask(task : Task)
25+
+ getSize() : int
26+
+ getTask() : Task
27+
}
28+
class WorkCenter {
29+
- leader : Worker
30+
- workers : List<Worker>
31+
+ WorkCenter()
32+
+ addWorker(worker : Worker)
33+
+ createWorkers(numberOfWorkers : int, taskSet : TaskSet, taskHandler : TaskHandler)
34+
+ getLeader() : Worker
35+
+ getWorkers() : List<Worker>
36+
+ promoteLeader()
37+
+ removeWorker(worker : Worker)
38+
}
39+
class Worker {
40+
- id : long
41+
- taskHandler : TaskHandler
42+
- taskSet : TaskSet
43+
- workCenter : WorkCenter
44+
+ Worker(id : long, workCenter : WorkCenter, taskSet : TaskSet, taskHandler : TaskHandler)
45+
+ equals(o : Object) : boolean
46+
+ hashCode() : int
47+
+ run()
48+
}
49+
}
50+
Worker --> "-taskSet" TaskSet
51+
Worker --> "-taskHandler" TaskHandler
52+
TaskSet --> "-queue" Task
53+
Worker --> "-workCenter" WorkCenter
54+
@enduml

leader-followers/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
27+
<modelVersion>4.0.0</modelVersion>
28+
<parent>
29+
<groupId>com.iluwatar</groupId>
30+
<artifactId>java-design-patterns</artifactId>
31+
<version>1.23.0-SNAPSHOT</version>
32+
</parent>
33+
<artifactId>leader-followers</artifactId>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.mockito</groupId>
42+
<artifactId>mockito-core</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.leaderfollowers;
25+
26+
import java.util.Random;
27+
import java.util.concurrent.Executors;
28+
import java.util.concurrent.TimeUnit;
29+
30+
/**
31+
* Leader/Followers pattern is a concurrency pattern. This pattern behaves like a taxi stand where
32+
* one of the threads acts as leader thread which listens for event from event sources,
33+
* de-multiplexes, dispatches and handles the event. It promotes the follower to be the new leader.
34+
* When processing completes the thread joins the followers queue, if there are no followers then it
35+
* becomes the leader and cycle repeats again.
36+
*
37+
* <p>In this example, one of the workers becomes Leader and listens on the {@link TaskSet} for
38+
* work. {@link TaskSet} basically acts as the source of input events for the {@link Worker}, who
39+
* are spawned and controlled by the {@link WorkCenter} . When {@link Task} arrives then the leader
40+
* takes the work and calls the {@link TaskHandler}. It also calls the {@link WorkCenter} to
41+
* promotes one of the followers to be the new leader, who can then process the next work and so
42+
* on.
43+
*
44+
* <p>The pros for this pattern are:
45+
* It enhances CPU cache affinity and eliminates unbound allocation and data buffer sharing between
46+
* threads by reading the request into buffer space allocated on the stack of the leader or by using
47+
* the Thread-Specific Storage pattern [22] to allocate memory. It minimizes locking overhead by not
48+
* exchanging data between threads, thereby reducing thread synchronization. In bound handle/thread
49+
* associations, the leader thread dispatches the event based on the I/O handle. It can minimize
50+
* priority inversion because no extra queuing is introduced in the server. It does not require a
51+
* context switch to handle each event, reducing the event dispatching latency. Note that promoting
52+
* a follower thread to fulfill the leader role requires a context switch. Programming simplicity:
53+
* The Leader/Followers pattern simplifies the programming of concurrency models where multiple
54+
* threads can receive requests, process responses, and de-multiplex connections using a shared
55+
* handle set.
56+
*/
57+
public class App {
58+
59+
/**
60+
* The main method for the leader followers pattern.
61+
*/
62+
public static void main(String[] args) throws InterruptedException {
63+
var taskSet = new TaskSet();
64+
var taskHandler = new TaskHandler();
65+
var workCenter = new WorkCenter();
66+
workCenter.createWorkers(4, taskSet, taskHandler);
67+
execute(workCenter, taskSet);
68+
}
69+
70+
/**
71+
* Start the work, dispatch tasks and stop the thread pool at last.
72+
*/
73+
private static void execute(WorkCenter workCenter, TaskSet taskSet) throws InterruptedException {
74+
var workers = workCenter.getWorkers();
75+
var exec = Executors.newFixedThreadPool(workers.size());
76+
workers.forEach(exec::submit);
77+
Thread.sleep(1000);
78+
addTasks(taskSet);
79+
exec.awaitTermination(2, TimeUnit.SECONDS);
80+
exec.shutdownNow();
81+
}
82+
83+
/**
84+
* Add tasks.
85+
*/
86+
private static void addTasks(TaskSet taskSet) throws InterruptedException {
87+
var rand = new Random();
88+
for (var i = 0; i < 5; i++) {
89+
var time = Math.abs(rand.nextInt(1000));
90+
taskSet.addTask(new Task(time));
91+
}
92+
}
93+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.leaderfollowers;
25+
26+
/**
27+
* A unit of work to be processed by the Workers.
28+
*/
29+
public class Task {
30+
31+
private final int time;
32+
33+
private boolean finished;
34+
35+
public Task(int time) {
36+
this.time = time;
37+
}
38+
39+
public int getTime() {
40+
return time;
41+
}
42+
43+
public void setFinished() {
44+
this.finished = true;
45+
}
46+
47+
public boolean isFinished() {
48+
return this.finished;
49+
}
50+
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.leaderfollowers;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* The TaskHandler is used by the {@link Worker} to process the newly arrived task.
31+
*/
32+
public class TaskHandler {
33+
34+
private static final Logger LOGGER = LoggerFactory.getLogger(TaskHandler.class);
35+
36+
/**
37+
* This interface handles one task at a time.
38+
*/
39+
public void handleTask(Task task) throws InterruptedException {
40+
var time = task.getTime();
41+
Thread.sleep(time);
42+
LOGGER.info("It takes " + time + " milliseconds to finish the task");
43+
task.setFinished();
44+
}
45+
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.leaderfollowers;
25+
26+
import java.util.concurrent.ArrayBlockingQueue;
27+
import java.util.concurrent.BlockingQueue;
28+
29+
/**
30+
* A TaskSet is a collection of the tasks, the leader receives task from here.
31+
*/
32+
public class TaskSet {
33+
34+
private BlockingQueue<Task> queue = new ArrayBlockingQueue<>(100);
35+
36+
public void addTask(Task task) throws InterruptedException {
37+
queue.put(task);
38+
}
39+
40+
public Task getTask() throws InterruptedException {
41+
return queue.take();
42+
}
43+
44+
public int getSize() {
45+
return queue.size();
46+
}
47+
}

0 commit comments

Comments
 (0)