Skip to content

Commit efc2c88

Browse files
authored
Merge pull request iluwatar#521 from Amarnath510/master
Issue iluwatar#450. First commit. Queue Based Load Leveling
2 parents faa29f8 + 1d6d0c5 commit efc2c88

16 files changed

+776
-5
lines changed

pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
THE SOFTWARE.
20-
-->
21-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2321
<modelVersion>4.0.0</modelVersion>
2422
<groupId>com.iluwatar</groupId>
2523
<artifactId>java-design-patterns</artifactId>
@@ -133,6 +131,7 @@
133131
<module>promise</module>
134132
<module>page-object</module>
135133
<module>event-asynchronous</module>
134+
<module>queue-load-leveling</module>
136135
<module>object-mother</module>
137136
</modules>
138137

@@ -312,7 +311,7 @@
312311
</goals>
313312
</pluginExecutionFilter>
314313
<action>
315-
<ignore></ignore>
314+
<ignore/>
316315
</action>
317316
</pluginExecution>
318317
</pluginExecutions>
@@ -466,4 +465,4 @@
466465
</plugins>
467466
</reporting>
468467

469-
</project>
468+
</project>

queue-load-leveling/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: pattern
3+
title: Queue based load leveling
4+
folder: queue-load-leveling
5+
permalink: /patterns/queue-load-leveling/
6+
pumlid:
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Intermediate
11+
- Performance
12+
---
13+
14+
## Intent
15+
Use a queue that acts as a buffer between a task and a service that it invokes in order to smooth
16+
intermittent heavy loads that may otherwise cause the service to fail or the task to time out.
17+
This pattern can help to minimize the impact of peaks in demand on availability and responsiveness
18+
for both the task and the service.
19+
20+
![alt text](./etc/queue-load-leveling.gif "queue-load-leveling")
21+
22+
23+
## Applicability
24+
25+
* This pattern is ideally suited to any type of application that uses services that may be subject to overloading.
26+
* This pattern might not be suitable if the application expects a response from the service with minimal latency.
27+
28+
## Real world example
29+
30+
* A Microsoft Azure web role stores data by using a separate storage service. If a large number of instances of the web role run concurrently, it is possible that the storage service could be overwhelmed and be unable to respond to requests quickly enough to prevent these requests from timing out or failing.
31+
32+
## Credits
33+
34+
* [Microsoft Cloud Design Patterns: Queue-Based Load Leveling Pattern](https://msdn.microsoft.com/en-us/library/dn589783.aspx)
14.7 KB
Loading
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.11" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="org.queue.load.leveling.TaskGenerator" project="queue-load-leveling"
5+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/TaskGenerator.java" binary="false"
6+
corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="470" y="213"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<interface id="2" language="java" name="org.queue.load.leveling.Task" project="queue-load-leveling"
15+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Task.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="-1" width="-1" x="426" y="389"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</interface>
23+
<class id="3" language="java" name="org.queue.load.leveling.MessageQueue" project="queue-load-leveling"
24+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/MessageQueue.java" binary="false"
25+
corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="661" y="419"/>
27+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
28+
sort-features="false" accessors="true" visibility="true">
29+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
30+
<operations public="true" package="true" protected="true" private="true" static="true"/>
31+
</display>
32+
</class>
33+
<class id="4" language="java" name="org.queue.load.leveling.Message" project="queue-load-leveling"
34+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/Message.java" binary="false" corner="BOTTOM_RIGHT">
35+
<position height="-1" width="-1" x="594" y="657"/>
36+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
37+
sort-features="false" accessors="true" visibility="true">
38+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
39+
<operations public="true" package="true" protected="true" private="true" static="true"/>
40+
</display>
41+
</class>
42+
<class id="5" language="java" name="org.queue.load.leveling.ServiceExecutor" project="queue-load-leveling"
43+
file="/queue-load-leveling/src/main/java/org/queue/load/leveling/ServiceExecutor.java" binary="false"
44+
corner="BOTTOM_RIGHT">
45+
<position height="-1" width="-1" x="775" y="193"/>
46+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
47+
sort-features="false" accessors="true" visibility="true">
48+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
49+
<operations public="true" package="true" protected="true" private="true" static="true"/>
50+
</display>
51+
</class>
52+
<realization id="6">
53+
<end type="SOURCE" refId="1"/>
54+
<end type="TARGET" refId="2"/>
55+
</realization>
56+
<association id="7">
57+
<end type="SOURCE" refId="1" navigable="false">
58+
<attribute id="8" name="msgQueue"/>
59+
<multiplicity id="9" minimum="0" maximum="1"/>
60+
</end>
61+
<end type="TARGET" refId="3" navigable="true"/>
62+
<display labels="true" multiplicity="true"/>
63+
</association>
64+
<association id="10">
65+
<end type="SOURCE" refId="3" navigable="false">
66+
<attribute id="11" name="blkQueue"/>
67+
<multiplicity id="12" minimum="0" maximum="2147483647"/>
68+
</end>
69+
<end type="TARGET" refId="4" navigable="true"/>
70+
<display labels="true" multiplicity="true"/>
71+
</association>
72+
<association id="13">
73+
<end type="SOURCE" refId="5" navigable="false">
74+
<attribute id="14" name="msgQueue"/>
75+
<multiplicity id="15" minimum="0" maximum="1"/>
76+
</end>
77+
<end type="TARGET" refId="3" navigable="true"/>
78+
<display labels="true" multiplicity="true"/>
79+
</association>
80+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
81+
sort-features="false" accessors="true" visibility="true">
82+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
83+
<operations public="true" package="true" protected="true" private="true" static="true"/>
84+
</classifier-display>
85+
<association-display labels="true" multiplicity="true"/>
86+
</class-diagram>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@startuml
2+
@enduml

queue-load-leveling/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 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"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.14.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>queue-load-leveling</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 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 org.queue.load.leveling;
25+
26+
import java.util.concurrent.ExecutorService;
27+
import java.util.concurrent.Executors;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
/**
34+
*
35+
* Many solutions in the cloud involve running tasks that invoke services. In this environment,
36+
* if a service is subjected to intermittent heavy loads, it can cause performance or reliability issues.
37+
* <p>
38+
* A service could be a component that is part of the same solution as the tasks that utilize it, or it
39+
* could be a third-party service providing access to frequently used resources such as a cache or a storage service.
40+
* If the same service is utilized by a number of tasks running concurrently, it can be difficult to predict the
41+
* volume of requests to which the service might be subjected at any given point in time.
42+
* <p>
43+
* We will build a queue-based-load-leveling to solve above problem.
44+
* Refactor the solution and introduce a queue between the task and the service.
45+
* The task and the service run asynchronously. The task posts a message containing the data required
46+
* by the service to a queue. The queue acts as a buffer, storing the message until it is retrieved
47+
* by the service. The service retrieves the messages from the queue and processes them.
48+
* Requests from a number of tasks, which can be generated at a highly variable rate, can be passed
49+
* to the service through the same message queue.
50+
* <p>
51+
* The queue effectively decouples the tasks from the service, and the service can handle the messages
52+
* at its own pace irrespective of the volume of requests from concurrent tasks. Additionally,
53+
* there is no delay to a task if the service is not available at the time it posts a message to the queue.
54+
* <p>
55+
* In this example we have a class {@link MessageQueue} to hold the message {@link Message} objects.
56+
* All the worker threads {@link TaskGenerator} will submit the messages to the MessageQueue.
57+
* The service executor class {@link ServiceExecutor} will pick up one task at a time from the Queue and
58+
* execute them.
59+
*
60+
*/
61+
public class App {
62+
63+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
64+
65+
//Executor shut down time limit.
66+
private static final int SHUTDOWN_TIME = 15;
67+
68+
/**
69+
* Program entry point
70+
*
71+
* @param args command line args
72+
*/
73+
public static void main(String[] args) {
74+
75+
// An Executor that provides methods to manage termination and methods that can
76+
// produce a Future for tracking progress of one or more asynchronous tasks.
77+
ExecutorService executor = null;
78+
79+
try {
80+
// Create a MessageQueue object.
81+
MessageQueue msgQueue = new MessageQueue();
82+
83+
LOGGER.info("Submitting TaskGenerators and ServiceExecutor threads.");
84+
85+
// Create three TaskGenerator threads. Each of them will submit different number of jobs.
86+
Runnable taskRunnable1 = new TaskGenerator(msgQueue, 5);
87+
Runnable taskRunnable2 = new TaskGenerator(msgQueue, 1);
88+
Runnable taskRunnable3 = new TaskGenerator(msgQueue, 2);
89+
90+
// Create e service which should process the submitted jobs.
91+
Runnable srvRunnable = new ServiceExecutor(msgQueue);
92+
93+
// Create a ThreadPool of 2 threads and
94+
// submit all Runnable task for execution to executor..
95+
executor = Executors.newFixedThreadPool(2);
96+
executor.submit(taskRunnable1);
97+
executor.submit(taskRunnable2);
98+
executor.submit(taskRunnable3);
99+
100+
// submitting serviceExecutor thread to the Executor service.
101+
executor.submit(srvRunnable);
102+
103+
// Initiates an orderly shutdown.
104+
LOGGER.info("Intiating shutdown. Executor will shutdown only after all the Threads are completed.");
105+
executor.shutdown();
106+
107+
// Wait for SHUTDOWN_TIME seconds for all the threads to complete
108+
// their tasks and then shut down the executor and then exit.
109+
if ( !executor.awaitTermination(SHUTDOWN_TIME, TimeUnit.SECONDS) ) {
110+
LOGGER.info("Executor was shut down and Exiting.");
111+
executor.shutdownNow();
112+
}
113+
} catch (InterruptedException ie) {
114+
LOGGER.error(ie.getMessage());
115+
} catch (Exception e) {
116+
LOGGER.error(e.getMessage());
117+
}
118+
}
119+
}
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 (c) 2014 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+
package org.queue.load.leveling;
24+
25+
/**
26+
* Message class with only one parameter.
27+
*
28+
*/
29+
public class Message {
30+
private final String msg;
31+
32+
// Parameter constructor.
33+
public Message(String msg) {
34+
super();
35+
this.msg = msg;
36+
}
37+
38+
// Get Method for attribute msg.
39+
public String getMsg() {
40+
return msg;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return msg;
46+
}
47+
}

0 commit comments

Comments
 (0)