Skip to content

Commit 17d019f

Browse files
authored
Merge branch 'iluwatar:master' into master
2 parents c2c13ae + cd224ea commit 17d019f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+98
-66
lines changed

leader-followers/src/main/java/com/iluwatar/leaderfollowers/App.java

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,13 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 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-
*/
251
package com.iluwatar.leaderfollowers;
262

273
import java.security.SecureRandom;
284
import java.util.concurrent.Executors;
295
import java.util.concurrent.TimeUnit;
6+
import lombok.extern.slf4j.Slf4j;
307

31-
/**
32-
* Leader/Followers pattern is a concurrency pattern. This pattern behaves like a taxi stand where
33-
* one of the threads acts as leader thread which listens for event from event sources,
34-
* de-multiplexes, dispatches and handles the event. It promotes the follower to be the new leader.
35-
* When processing completes the thread joins the followers queue, if there are no followers then it
36-
* becomes the leader and cycle repeats again.
37-
*
38-
* <p>In this example, one of the workers becomes Leader and listens on the {@link TaskSet} for
39-
* work. {@link TaskSet} basically acts as the source of input events for the {@link Worker}, who
40-
* are spawned and controlled by the {@link WorkCenter} . When {@link Task} arrives then the leader
41-
* takes the work and calls the {@link TaskHandler}. It also calls the {@link WorkCenter} to
42-
* promotes one of the followers to be the new leader, who can then process the next work and so on.
43-
*
44-
* <p>The pros for this pattern are: It enhances CPU cache affinity and eliminates unbound
45-
* allocation and data buffer sharing between threads by reading the request into buffer space
46-
* allocated on the stack of the leader or by using the Thread-Specific Storage pattern [22] to
47-
* allocate memory. It minimizes locking overhead by not exchanging data between threads, thereby
48-
* reducing thread synchronization. In bound handle/thread associations, the leader thread
49-
* dispatches the event based on the I/O handle. It can minimize priority inversion because no extra
50-
* queuing is introduced in the server. It does not require a context switch to handle each event,
51-
* reducing the event dispatching latency. Note that promoting a follower thread to fulfill the
52-
* leader role requires a context switch. Programming simplicity: The Leader/Followers pattern
53-
* simplifies the programming of concurrency models where multiple threads can receive requests,
54-
* process responses, and de-multiplex connections using a shared handle set.
55-
*/
8+
@Slf4j
569
public class App {
5710

58-
/** The main method for the leader followers pattern. */
5911
public static void main(String[] args) throws InterruptedException {
6012
var taskSet = new TaskSet();
6113
var taskHandler = new TaskHandler();
@@ -64,18 +16,23 @@ public static void main(String[] args) throws InterruptedException {
6416
execute(workCenter, taskSet);
6517
}
6618

67-
/** Start the work, dispatch tasks and stop the thread pool at last. */
6819
private static void execute(WorkCenter workCenter, TaskSet taskSet) throws InterruptedException {
6920
var workers = workCenter.getWorkers();
7021
var exec = Executors.newFixedThreadPool(workers.size());
71-
workers.forEach(exec::submit);
72-
Thread.sleep(1000);
73-
addTasks(taskSet);
74-
exec.awaitTermination(2, TimeUnit.SECONDS);
75-
exec.shutdownNow();
22+
23+
try {
24+
workers.forEach(exec::submit);
25+
Thread.sleep(1000);
26+
addTasks(taskSet);
27+
boolean terminated = exec.awaitTermination(2, TimeUnit.SECONDS);
28+
if (!terminated) {
29+
LOGGER.warn("Executor did not terminate in the given time.");
30+
}
31+
} finally {
32+
exec.shutdownNow();
33+
}
7634
}
7735

78-
/** Add tasks. */
7936
private static void addTasks(TaskSet taskSet) throws InterruptedException {
8037
var rand = new SecureRandom();
8138
for (var i = 0; i < 5; i++) {

saga/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Wikipedia says
3131

3232
> Long-running transactions (also known as the saga interaction pattern) are computer database transactions that avoid locks on non-local resources, use compensation to handle failures, potentially aggregate smaller ACID transactions (also referred to as atomic transactions), and typically use a coordinator to complete or abort the transaction. In contrast to rollback in ACID transactions, compensation restores the original state, or an equivalent, and is business-specific. For example, the compensating action for making a hotel reservation is canceling that reservation.
3333
34+
Flowchart
35+
36+
![Saga flowchart](./etc/saga-flowchart.png)
37+
3438
## Programmatic Example of Saga Pattern in Java
3539

3640
The Saga design pattern is a sequence of local transactions where each transaction updates data within a single service. It's particularly useful in a microservices architecture where each service has its own database. The Saga pattern ensures data consistency and fault tolerance across services. Here are the key components of the Saga pattern:

saga/etc/saga-flowchart.png

70.5 KB
Loading

separated-interface/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ In plain words
3131

3232
> Defines a client interface separate from its implementation to allow for flexible and interchangeable components.
3333
34+
Sequence diagram
35+
36+
![Separated Interface sequence diagram](./etc/separated-interface-sequence-diagram.png)
37+
3438
## Programmatic Example of Separated Interface Pattern in Java
3539

3640
The Java Separated Interface design pattern is a crucial software architecture strategy that promotes separating the interface definition from its implementation, crucial for enhancing system flexibility and scalability. This allows the client to be completely unaware of the implementation, promoting loose coupling and enhancing flexibility.
28.5 KB
Loading

serialized-entity/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Wikipedia says
3232

3333
> In computing, serialization is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (e.g. data streams over computer networks) and reconstructed later (possibly in a different computer environment). When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of objects does not include any of their associated methods with which they were previously linked.
3434
35+
Flowchart
36+
37+
![Serialized Entity flowchart](./etc/serialized-entity-flowchart.png)
38+
3539
## Programmatic Example of Serialized Entity Pattern in Java
3640

3741
The Serialized Entity design pattern is a way to easily persist Java objects to the database. It uses the `Serializable` interface and the DAO (Data Access Object) pattern. The pattern first uses `Serializable` to convert a Java object into a set of bytes, then it uses the DAO pattern to store this set of bytes as a BLOB (Binary Large OBject) in the database.
32.2 KB
Loading

serialized-lob/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ In plain words
3030

3131
> The Serialized LOB design pattern manages the storage of large objects, such as files or multimedia, by serializing and storing them directly within a database.
3232
33+
Flowchart
34+
35+
![Serialized LOB flowchart](./etc/serialized-lob-flowchart.png)
36+
3337
## Programmatic Example of Serialized LOB Pattern in Java
3438

3539
The Serialized Large Object (LOB) design pattern is a way to handle large objects in a database. It involves serializing an object graph into a single large object (a BLOB or CLOB, for Binary Large Object or Character Large Object, respectively) and storing it in the database. When the object graph needs to be retrieved, it is read from the database and deserialized back into the original object graph.
39.5 KB
Loading

servant/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Wikipedia says
3434

3535
> In software engineering, the servant pattern defines an object used to offer some functionality to a group of classes without defining that functionality in each of them. A Servant is a class whose instance (or even just class) provides methods that take care of a desired service, while objects for which (or with whom) the servant does something, are taken as parameters.
3636
37+
Sequence diagram
38+
39+
![Servant sequence diagram](./etc/servant-sequence-diagram.png)
40+
3741
## Programmatic Example of Servant Pattern in Java
3842

3943
The Servant design pattern is a behavioral design pattern that defines a class that provides some sort of service to a group of classes. This pattern is particularly useful when these classes lack some common functionality that can't be added to the superclass. The Servant class brings this common functionality to a group of classes.

0 commit comments

Comments
 (0)