ResourcePooler.java
14.1 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.jmx.statistics.BoundedRangeStatistic
* com.adobe.jmx.statistics.CountStatistic
* com.adobe.jmx.statistics.RangeStatistic
* com.adobe.jmx.statistics.StatisticsHost
* com.adobe.jmx.statistics.TimeStatistic
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.service;
import com.adobe.jmx.statistics.*;
import com.adobe.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
class ResourcePooler {
public static final boolean tracePoolerActivity = System.getProperty("com.adobe.bmc.tracePoolerActivity") != null && System.getProperty("com.adobe.bmc.tracePoolerActivity").trim().equalsIgnoreCase("true");
private static final Logger logger = LoggerFactory.getLogger(ResourcePooler.class);
private static final int MAX_STRIKES = 5;
private static int strikes = 0;
private ArrayList pool = new ArrayList();
private boolean initializingResource = false;
private ConnectionFactoryManager connectionFactoryManager;
BoundedRangeStatistic poolSize;
RangeStatistic queueLength;
RangeStatistic inUse;
CountStatistic requestCount;
TimeStatistic holdTime;
private boolean isShutdown = false;
private WaitItem waitQueueTail = null;
private static long nextSeq = 0;
ResourcePooler(ConnectionFactoryManager connectionFactoryMgr) {
this.connectionFactoryManager = connectionFactoryMgr;
ServiceAPI host = this.connectionFactoryManager.service;
this.poolSize = BoundedRangeStatistic.create((StatisticsHost)host, (String)"PoolSize");
this.poolSize.setBounds(0, 0);
this.queueLength = RangeStatistic.create((StatisticsHost)host, (String)"QueueLength");
this.inUse = RangeStatistic.create((StatisticsHost)host, (String)"InUse");
this.requestCount = CountStatistic.create((StatisticsHost)host, (String)"RequestCount");
this.holdTime = TimeStatistic.create((StatisticsHost)host, (String)"HoldTime");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
ConnectionResource allocateResource() {
WaitItem waitItem = null;
while (!this.isShutdown) {
Object resource;
ConnectionResource resource2;
logger.debug("*****" + Thread.currentThread().getName() + " trying to obtain lock on " + this + " for resource allocation");
ResourcePooler resourcePooler = this;
synchronized (resourcePooler) {
if (this.connectionFactoryManager.service.getState() == ServiceAPI.SERVICE_FAILED) {
if (strikes < 5) {
logger.info("Failure while process launch, attempt: " + ++strikes);
} else {
strikes = 0;
throw new IllegalStateException("Connection to failed service.");
}
}
if (this.pool.size() > 0) {
ListIterator iter = this.pool.listIterator();
while (iter.hasNext()) {
resource = (ConnectionResource)iter.next();
ResourcePeer peer = resource.peer;
if (!peer.allocated) {
peer.allocated = true;
strikes = 0;
peer.setReusable(false);
peer.requestStartTime = this.holdTime.begin();
this.inUse.increment();
this.requestCount.increment();
logger.debug("*****" + Thread.currentThread().getName() + " allocated " + resource + ", Pool: " + ResourcePooler.getPoolStateMap(this) + ", initializing=" + this.initializingResource + ", poolsize=" + this.pool.size() + ", max=" + this.poolSize.getUpperBound());
return resource;
}
logger.debug("*****" + Thread.currentThread().getName() + " " + resource + " is already allocated" + ", Pool: " + ResourcePooler.getPoolStateMap(this) + ", initializing=" + this.initializingResource + ", poolsize=" + this.pool.size() + ", max=" + this.poolSize.getUpperBound());
}
}
long max = this.poolSize.getUpperBound();
if (this.initializingResource || max != 0 && (long)this.pool.size() >= max) {
logger.debug("*****" + Thread.currentThread().getName() + " waiting for resource; initializing=" + this.initializingResource + ", poolsize=" + this.pool.size() + ", max=" + max);
waitItem = this.waitForResource(waitItem);
continue;
}
this.initializingResource = true;
}
try {
resource2 = this.connectionFactoryManager.createConnectionResource();
}
catch (RuntimeException e) {
logger.warn("Unexpected error creating connection resource", (Throwable)e);
resource = this;
synchronized (resource) {
this.initializingResource = false;
}
throw e;
}
ResourcePeer peer = resource2.peer;
peer.pooler = this;
peer.setReusable(true);
logger.debug("*****" + Thread.currentThread().getName() + " created resource " + resource2 + ", now check for readiness");
resource = this;
synchronized (resource) {
if (resource2.peer.ready) {
ResourcePooler.notifyReady(resource2);
} else {
logger.debug("*****" + Thread.currentThread().getName() + " newly created resource " + resource2 + " not ready yet; initializing=" + this.initializingResource + ", poolsize=" + this.pool.size() + ", max=" + this.poolSize.getUpperBound());
}
continue;
}
}
throw new IllegalStateException("Service is shutdown");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
void resetResources() {
ResourcePooler resourcePooler = this;
synchronized (resourcePooler) {
ListIterator iter = this.pool.listIterator();
while (iter.hasNext()) {
Resource resource = (Resource)iter.next();
logger.info("Shutting down resource: " + resource);
resource.onShutdown();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void onShutdown() {
logger.debug("Pooler {} for service {} shutting down", (Object)this, (Object)this.connectionFactoryManager.service.getName());
ResourcePooler resourcePooler = this;
synchronized (resourcePooler) {
this.isShutdown = true;
this.NotifyAllInQueue();
}
resourcePooler = this;
synchronized (resourcePooler) {
ListIterator iter = this.pool.listIterator();
while (iter.hasNext()) {
Resource resource = (Resource)iter.next();
resource.onShutdown();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
static void onDeallocate(Resource resource) {
ResourcePeer peer = resource.peer;
ResourcePooler pooler = peer.pooler;
if (pooler != null) {
ResourcePooler resourcePooler = pooler;
synchronized (resourcePooler) {
if (peer.requestStartTime != 0) {
pooler.inUse.decrement();
pooler.holdTime.end(peer.requestStartTime);
peer.requestStartTime = 0;
}
peer.allocated = false;
if (!peer.reusable) {
if (resource instanceof ProcessResource) {
((ProcessResource)resource).releaseAgent();
}
pooler.pool.remove(resource);
pooler.poolSize.setCurrent((long)pooler.pool.size());
}
if (!peer.ready) {
pooler.initializingResource = false;
pooler.connectionFactoryManager.service.setState(ServiceAPI.SERVICE_FAILED);
logger.debug("*****" + Thread.currentThread().getName() + " deallocated *FAILED RESOURCE* " + resource + ", Pool: " + ResourcePooler.getPoolStateMap(pooler) + ", initializing=" + pooler.initializingResource + ", poolsize=" + pooler.pool.size() + ", max=" + pooler.poolSize.getUpperBound() + ", service " + pooler.connectionFactoryManager.service.getName() + " marked FAILED.");
pooler.NotifyAllInQueue();
} else {
logger.debug("*****" + Thread.currentThread().getName() + " deallocated " + resource + ", Pool: " + ResourcePooler.getPoolStateMap(pooler) + ", initializing=" + pooler.initializingResource + ", poolsize=" + pooler.pool.size() + ", max=" + pooler.poolSize.getUpperBound());
pooler.NotifyFirstInQueue();
}
}
} else {
peer.allocated = false;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
static void notifyReady(Resource resource) {
ResourcePeer peer = resource.peer;
ResourcePooler pooler = peer.pooler;
if (pooler != null) {
ResourcePooler resourcePooler = pooler;
synchronized (resourcePooler) {
pooler.initializingResource = false;
if (!pooler.pool.contains(resource)) {
pooler.pool.add(resource);
pooler.poolSize.setCurrent((long)pooler.pool.size());
}
logger.debug("*****" + Thread.currentThread().getName() + " resource " + resource + " notifying readiness, Pool: " + ResourcePooler.getPoolStateMap(pooler) + ", initializing=" + pooler.initializingResource + ", poolsize=" + pooler.pool.size() + ", max=" + pooler.poolSize.getUpperBound());
pooler.NotifyAllInQueue();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private WaitItem waitForResource(WaitItem waitItem) {
if (waitItem == null) {
waitItem = new WaitItem(nextSeq++);
if (this.waitQueueTail == null) {
waitItem.next = waitItem;
this.waitQueueTail = waitItem;
} else {
waitItem.next = this.waitQueueTail.next;
this.waitQueueTail.next = waitItem;
this.waitQueueTail = waitItem;
}
} else if (this.waitQueueTail == null) {
waitItem.next = waitItem;
this.waitQueueTail = waitItem;
} else {
WaitItem item = this.waitQueueTail.next;
while (item != this.waitQueueTail && item.next.seq < waitItem.seq) {
item = item.next;
}
waitItem.next = item.next;
item.next = waitItem;
if (item == this.waitQueueTail) {
this.waitQueueTail = waitItem;
}
}
try {
this.queueLength.increment();
this.wait();
}
catch (InterruptedException e) {
try {
logger.debug("Interrupted waiting for resource", (Throwable)e);
}
catch (Throwable var3_5) {
this.queueLength.decrement();
if (waitItem.next != null) {
WaitItem prev = this.waitQueueTail;
while (prev.next != waitItem) {
prev = prev.next;
}
prev.next = waitItem.next;
if (this.waitQueueTail == waitItem) {
this.waitQueueTail = null;
}
waitItem.next = null;
}
throw var3_5;
}
this.queueLength.decrement();
if (waitItem.next != null) {
WaitItem prev = this.waitQueueTail;
while (prev.next != waitItem) {
prev = prev.next;
}
prev.next = waitItem.next;
if (this.waitQueueTail == waitItem) {
this.waitQueueTail = null;
}
waitItem.next = null;
}
}
this.queueLength.decrement();
if (waitItem.next != null) {
WaitItem prev = this.waitQueueTail;
while (prev.next != waitItem) {
prev = prev.next;
}
prev.next = waitItem.next;
if (this.waitQueueTail == waitItem) {
this.waitQueueTail = null;
}
waitItem.next = null;
}
return waitItem;
}
private synchronized void NotifyFirstInQueue() {
if (this.waitQueueTail != null) {
WaitItem item = this.waitQueueTail.next;
this.waitQueueTail.next = item.next;
if (item == this.waitQueueTail) {
this.waitQueueTail = null;
}
item.next = null;
item.thread.interrupt();
}
}
private synchronized void NotifyAllInQueue() {
while (this.waitQueueTail != null) {
this.NotifyFirstInQueue();
}
}
private static Map getPoolStateMap(ResourcePooler pooler) {
HashMap<ConnectionResource, Boolean> poolState = new HashMap<ConnectionResource, Boolean>();
for (int i = 0; i < pooler.pool.size(); ++i) {
ConnectionResource cr = (ConnectionResource)pooler.pool.get(i);
poolState.put(cr, cr.peer.allocated);
}
return poolState;
}
private static class WaitItem {
final long seq;
final Thread thread;
WaitItem next;
WaitItem(long nextSeq) {
this.seq = nextSeq;
this.thread = Thread.currentThread();
}
}
}