|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| package org.objectledge.pipeline; |
|
29 |
| |
|
30 |
| import java.util.concurrent.Semaphore; |
|
31 |
| |
|
32 |
| import org.jcontainer.dna.Configuration; |
|
33 |
| import org.jcontainer.dna.ConfigurationException; |
|
34 |
| import org.objectledge.context.Context; |
|
35 |
| import org.objectledge.statistics.DataSource; |
|
36 |
| import org.objectledge.statistics.Graph; |
|
37 |
| import org.objectledge.statistics.ReflectiveStatisticsProvider; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class ConcurrencyControlValve |
|
46 |
| extends ReflectiveStatisticsProvider |
|
47 |
| implements Valve |
|
48 |
| { |
|
49 |
| private final Valve nestedValve; |
|
50 |
| |
|
51 |
| private final Semaphore semaphore; |
|
52 |
| |
|
53 |
| private final int limit; |
|
54 |
| |
|
55 |
| private volatile int threadCount = 0; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
0
| public ConcurrencyControlValve(final Valve nestedValve, final int limit)
|
|
64 |
| { |
|
65 |
0
| this.nestedValve = nestedValve;
|
|
66 |
0
| this.limit = limit;
|
|
67 |
0
| if(limit > 0)
|
|
68 |
| { |
|
69 |
0
| semaphore = new Semaphore(limit, true);
|
|
70 |
| } |
|
71 |
| else |
|
72 |
| { |
|
73 |
0
| semaphore = null;
|
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| public ConcurrencyControlValve(final Valve nestedValve, final Configuration config)
|
|
85 |
| throws ConfigurationException |
|
86 |
| { |
|
87 |
0
| this(nestedValve, config.getChild("limit").getValueAsInteger());
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
0
| public void process(Context context)
|
|
94 |
| throws ProcessingException |
|
95 |
| { |
|
96 |
0
| if(semaphore != null)
|
|
97 |
| { |
|
98 |
0
| try
|
|
99 |
| { |
|
100 |
0
| semaphore.acquireUninterruptibly();
|
|
101 |
0
| nestedValve.process(context);
|
|
102 |
| } |
|
103 |
| finally |
|
104 |
| { |
|
105 |
0
| semaphore.release();
|
|
106 |
| } |
|
107 |
| } |
|
108 |
| else |
|
109 |
| { |
|
110 |
0
| try
|
|
111 |
| { |
|
112 |
0
| threadCount++;
|
|
113 |
0
| nestedValve.process(context);
|
|
114 |
| } |
|
115 |
| finally |
|
116 |
| { |
|
117 |
0
| threadCount--;
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
0
| public String getName()
|
|
128 |
| { |
|
129 |
0
| return "Concurrency control";
|
|
130 |
| } |
|
131 |
| |
|
132 |
| private static final DataSource[] DATA_SOURCES = { |
|
133 |
| new DataSource("concurrency_threads_running", "Running threads", |
|
134 |
| DataSource.Type.GAUGE, DataSource.Graph.LINE1), |
|
135 |
| new DataSource("concurrency_threads_waiting", "Waiting threads", |
|
136 |
| DataSource.Type.GAUGE, DataSource.Graph.LINE1) }; |
|
137 |
| |
|
138 |
| private static final Graph[] GRAPHS = { new Graph("concurrency", "Execution concurrency", null, |
|
139 |
| DATA_SOURCES, "threads") }; |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
0
| public Graph[] getGraphs()
|
|
145 |
| { |
|
146 |
0
| return GRAPHS;
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
0
| public DataSource[] getDataSources()
|
|
153 |
| { |
|
154 |
0
| return DATA_SOURCES;
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
0
| public Number getConcurrencyThreadsRunning()
|
|
163 |
| { |
|
164 |
0
| if(semaphore != null)
|
|
165 |
| { |
|
166 |
0
| return new Integer(limit - semaphore.availablePermits());
|
|
167 |
| } |
|
168 |
| else |
|
169 |
| { |
|
170 |
0
| return threadCount;
|
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
0
| public Number getConcurrencyThreadsWaiting()
|
|
180 |
| { |
|
181 |
0
| if(semaphore != null)
|
|
182 |
| { |
|
183 |
0
| return new Integer(semaphore.getQueueLength());
|
|
184 |
| } |
|
185 |
| else |
|
186 |
| { |
|
187 |
0
| return 0;
|
|
188 |
| } |
|
189 |
| } |
|
190 |
| } |