-
Notifications
You must be signed in to change notification settings - Fork 290
/
ObjectPoolIssue326.java
182 lines (167 loc) · 7.27 KB
/
ObjectPoolIssue326.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.pool2;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
/**
* On my box with 4 cores this test fails at between 5s and 900s with an average
* of 240s (data from 10 runs of test).
*
* It is hard to turn this in a unit test because it can affect the build
* negatively since you need to run it for a while.
*/
public final class ObjectPoolIssue326 {
public static void main(final String[] args) {
try {
new ObjectPoolIssue326().run();
} catch (final Exception e) {
e.printStackTrace();
}
}
private void run() throws Exception {
final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMaxTotalPerKey(5);
poolConfig.setMinIdlePerKey(-1);
poolConfig.setMaxIdlePerKey(-1);
poolConfig.setLifo(true);
poolConfig.setFairness(true);
poolConfig.setMaxWaitMillis(30 * 1000);
poolConfig.setMinEvictableIdleTimeMillis(-1);
poolConfig.setSoftMinEvictableIdleTimeMillis(-1);
poolConfig.setNumTestsPerEvictionRun(1);
poolConfig.setTestOnCreate(false);
poolConfig.setTestOnBorrow(false);
poolConfig.setTestOnReturn(false);
poolConfig.setTestWhileIdle(false);
poolConfig.setTimeBetweenEvictionRunsMillis(5 * 1000);
poolConfig.setEvictionPolicyClassName(BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
poolConfig.setBlockWhenExhausted(false);
poolConfig.setJmxEnabled(false);
poolConfig.setJmxNameBase(null);
poolConfig.setJmxNamePrefix(null);
final GenericKeyedObjectPool<Integer, Object> pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig);
// number of threads to reproduce is finicky. this count seems to be best for my
// 4 core box.
// too many doesn't reproduce it ever, too few doesn't either.
final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
final long startTimeMillis = System.currentTimeMillis();
long testIter = 0;
try {
while (true) {
testIter++;
if (testIter % 1000 == 0) {
System.out.println(testIter);
}
final List<Task> tasks = createTasks(pool);
final List<Future<Object>> futures = service.invokeAll(tasks);
for (final Future<Object> future : futures) {
future.get();
}
}
} finally {
System.out.println("Time: " + (System.currentTimeMillis() - startTimeMillis) / 1000.0);
service.shutdown();
}
}
private List<Task> createTasks(final GenericKeyedObjectPool<Integer, Object> pool) {
final List<Task> tasks = new ArrayList<>();
for (int i = 0; i < 250; i++) {
tasks.add(new Task(pool, i));
}
return tasks;
}
private class ObjectFactory extends BaseKeyedPooledObjectFactory<Integer, Object> {
@Override
public Object create(final Integer s) throws Exception {
return new TestObject();
}
@Override
public PooledObject<Object> wrap(final Object o) {
return new DefaultPooledObject<>(o);
}
}
private class TestObject {
}
private class Task implements Callable<Object> {
private final GenericKeyedObjectPool<Integer, Object> m_pool;
private final int m_key;
Task(final GenericKeyedObjectPool<Integer, Object> pool, final int count) {
m_pool = pool;
m_key = count % 20;
}
@Override
public Object call() throws Exception {
try {
final Object value;
value = m_pool.borrowObject(m_key);
// don't make this too long or it won't reproduce, and don't make it zero or it
// won't reproduce
// constant low value also doesn't reproduce
busyWait(System.currentTimeMillis() % 4);
m_pool.returnObject(m_key, value);
return "success";
} catch (final NoSuchElementException e) {
// ignore, we've exhausted the pool
// not sure whether what we do here matters for reproducing
busyWait(System.currentTimeMillis() % 20);
return "exhausted";
}
}
private void busyWait(final long timeMillis) {
// busy waiting intentionally as a simple thread.sleep fails to reproduce
final long endTimeMillis = System.currentTimeMillis() + timeMillis;
while (System.currentTimeMillis() < endTimeMillis) {
// empty
}
}
}
}
/*
*
* Example stack trace: java.util.concurrent.ExecutionException:
* java.lang.NullPointerException at
* java.util.concurrent.FutureTask.report(FutureTask.java:122) at
* java.util.concurrent.FutureTask.get(FutureTask.java:192) at
* threading_pool.ObjectPoolIssue.run(ObjectPoolIssue.java:63) at
* threading_pool.ObjectPoolIssue.main(ObjectPoolIssue.java:23) at
* sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
* sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
* at
* sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
* java:43) at java.lang.reflect.Method.invoke(Method.java:498) at
* com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Caused
* by: java.lang.NullPointerException at
* org.apache.commons.pool2.impl.GenericKeyedObjectPool.returnObject(
* GenericKeyedObjectPool.java:474) at
* threading_pool.ObjectPoolIssue$Task.call(ObjectPoolIssue.java:112) at
* java.util.concurrent.FutureTask.run(FutureTask.java:266) at
* java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:
* 1142) at
* java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:
* 617) at java.lang.Thread.run(Thread.java:745)
*
*/