-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ThreadPoolBudget.java
195 lines (173 loc) · 5.42 KB
/
ThreadPoolBudget.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
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import java.io.Closeable;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* <p>A budget of required thread usage, used to warn or error for insufficient configured threads.</p>
*
* @see ThreadPool.SizedThreadPool#getThreadPoolBudget()
*/
@ManagedObject
public class ThreadPoolBudget
{
private static final Logger LOG = Log.getLogger(ThreadPoolBudget.class);
public interface Lease extends Closeable
{
int getThreads();
}
/**
* An allocation of threads
*/
public class Leased implements Lease
{
private final Object leasee;
private final int threads;
private Leased(Object leasee, int threads)
{
this.leasee = leasee;
this.threads = threads;
}
@Override
public int getThreads()
{
return threads;
}
@Override
public void close()
{
leases.remove(this);
warned.set(false);
}
}
private static final Lease NOOP_LEASE = new Lease()
{
@Override
public void close()
{
}
@Override
public int getThreads()
{
return 0;
}
};
private final Set<Leased> leases = new CopyOnWriteArraySet<>();
private final AtomicBoolean warned = new AtomicBoolean();
private final ThreadPool.SizedThreadPool pool;
private final int warnAt;
/**
* Construct a budget for a SizedThreadPool.
*
* @param pool The pool to budget thread allocation for.
*/
public ThreadPoolBudget(ThreadPool.SizedThreadPool pool)
{
this.pool = pool;
this.warnAt = -1;
}
/**
* @param pool The pool to budget thread allocation for.
* @param warnAt The level of free threads at which a warning is generated.
*/
@Deprecated
public ThreadPoolBudget(ThreadPool.SizedThreadPool pool, int warnAt)
{
this.pool = pool;
this.warnAt = warnAt;
}
public ThreadPool.SizedThreadPool getSizedThreadPool()
{
return pool;
}
@ManagedAttribute("the number of threads leased to components")
public int getLeasedThreads()
{
return leases.stream()
.mapToInt(Lease::getThreads)
.sum();
}
public void reset()
{
leases.clear();
warned.set(false);
}
public Lease leaseTo(Object leasee, int threads)
{
Leased lease = new Leased(leasee, threads);
leases.add(lease);
try
{
check(pool.getMaxThreads());
return lease;
}
catch (IllegalStateException e)
{
lease.close();
throw e;
}
}
/**
* <p>Checks leases against the given number of {@code maxThreads}.</p>
*
* @param maxThreads A proposed change to the maximum threads to check.
* @return true if passes check, false if otherwise (see logs for details)
* @throws IllegalStateException if insufficient threads are configured.
*/
public boolean check(int maxThreads) throws IllegalStateException
{
int required = getLeasedThreads();
int left = maxThreads - required;
if (left <= 0)
{
printInfoOnLeases();
throw new IllegalStateException(String.format("Insufficient configured threads: required=%d < max=%d for %s", required, maxThreads, pool));
}
if (left < warnAt)
{
if (warned.compareAndSet(false, true))
{
printInfoOnLeases();
LOG.info("Low configured threads: (max={} - required={})={} < warnAt={} for {}", maxThreads, required, left, warnAt, pool);
}
return false;
}
return true;
}
private void printInfoOnLeases()
{
leases.forEach(lease -> LOG.info("{} requires {} threads from {}", lease.leasee, lease.getThreads(), pool));
}
public static Lease leaseFrom(Executor executor, Object leasee, int threads)
{
if (executor instanceof ThreadPool.SizedThreadPool)
{
ThreadPoolBudget budget = ((ThreadPool.SizedThreadPool)executor).getThreadPoolBudget();
if (budget != null)
return budget.leaseTo(leasee, threads);
}
return NOOP_LEASE;
}
}