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 337 338 339 340 341
|
/*
* 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.tomcat.dbcp.pool2.impl;
import java.io.PrintWriter;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Deque;
import org.apache.tomcat.dbcp.pool2.PooledObject;
import org.apache.tomcat.dbcp.pool2.PooledObjectState;
import org.apache.tomcat.dbcp.pool2.TrackedUse;
/**
* This wrapper is used to track the additional information, such as state, for
* the pooled objects.
* <p>
* This class is intended to be thread-safe.
* </p>
*
* @param <T> the type of object in the pool
* @since 2.0
*/
public class DefaultPooledObject<T> implements PooledObject<T> {
private final T object;
private PooledObjectState state = PooledObjectState.IDLE; // @GuardedBy("this") to ensure transitions are valid
private final Clock systemClock = Clock.systemUTC();
private final Instant createInstant = now();
private volatile Instant lastBorrowInstant = createInstant;
private volatile Instant lastUseInstant = createInstant;
private volatile Instant lastReturnInstant = createInstant;
private volatile boolean logAbandoned;
private volatile CallStack borrowedBy = NoOpCallStack.INSTANCE;
private volatile CallStack usedBy = NoOpCallStack.INSTANCE;
private volatile long borrowedCount;
/**
* Creates a new instance that wraps the provided object so that the pool can
* track the state of the pooled object.
*
* @param object The object to wrap
*/
public DefaultPooledObject(final T object) {
this.object = object;
}
/**
* Allocates the object.
*
* @return {@code true} if the original state was {@link PooledObjectState#IDLE IDLE}
*/
@Override
public synchronized boolean allocate() {
if (state == PooledObjectState.IDLE) {
state = PooledObjectState.ALLOCATED;
lastBorrowInstant = now();
lastUseInstant = lastBorrowInstant;
borrowedCount++;
if (logAbandoned) {
borrowedBy.fillInStackTrace();
}
return true;
}
if (state == PooledObjectState.EVICTION) {
// TODO Allocate anyway and ignore eviction test
state = PooledObjectState.EVICTION_RETURN_TO_HEAD;
}
// TODO if validating and testOnBorrow == true then pre-allocate for
// performance
return false;
}
@Override
public int compareTo(final PooledObject<T> other) {
final int compareTo = getLastReturnInstant().compareTo(other.getLastReturnInstant());
if (compareTo == 0) {
// Make sure the natural ordering is broadly consistent with equals
// although this will break down if distinct objects have the same
// identity hash code.
// see java.lang.Comparable Javadocs
return System.identityHashCode(this) - System.identityHashCode(other);
}
return compareTo;
}
/**
* Deallocates the object and sets it {@link PooledObjectState#IDLE IDLE}
* if it is currently {@link PooledObjectState#ALLOCATED ALLOCATED}
* or {@link PooledObjectState#RETURNING RETURNING}.
*
* @return {@code true} if the state was {@link PooledObjectState#ALLOCATED ALLOCATED}
* or {@link PooledObjectState#RETURNING RETURNING}.
*/
@Override
public synchronized boolean deallocate() {
if (state == PooledObjectState.ALLOCATED || state == PooledObjectState.RETURNING) {
state = PooledObjectState.IDLE;
lastReturnInstant = now();
borrowedBy.clear();
return true;
}
return false;
}
@Override
public synchronized boolean endEvictionTest(
final Deque<PooledObject<T>> idleQueue) {
if (state == PooledObjectState.EVICTION) {
state = PooledObjectState.IDLE;
return true;
}
if (state == PooledObjectState.EVICTION_RETURN_TO_HEAD) {
state = PooledObjectState.IDLE;
idleQueue.offerFirst(this);
}
return false;
}
@Override
public long getActiveTimeMillis() {
return getActiveDuration().toMillis();
}
/**
* Gets the number of times this object has been borrowed.
* @return The number of times this object has been borrowed.
* @since 2.1
*/
@Override
public long getBorrowedCount() {
return borrowedCount;
}
@Override
public Instant getCreateInstant() {
return createInstant;
}
@Override
public long getCreateTime() {
return createInstant.toEpochMilli();
}
@Override
public Duration getIdleDuration() {
// elapsed may be negative if:
// - another thread updates lastReturnInstant during the calculation window
// - System.currentTimeMillis() is not monotonic (e.g. system time is set back)
final Duration elapsed = Duration.between(lastReturnInstant, now());
return elapsed.isNegative() ? Duration.ZERO : elapsed;
}
@Override
public Duration getIdleTime() {
return getIdleDuration();
}
@Override
public long getIdleTimeMillis() {
return getIdleDuration().toMillis();
}
@Override
public Instant getLastBorrowInstant() {
return lastBorrowInstant;
}
@Override
public long getLastBorrowTime() {
return lastBorrowInstant.toEpochMilli();
}
@Override
public Instant getLastReturnInstant() {
return lastReturnInstant;
}
@Override
public long getLastReturnTime() {
return lastReturnInstant.toEpochMilli();
}
/**
* Gets an estimate of the last time this object was used. If the class
* of the pooled object implements {@link TrackedUse}, what is returned is
* the maximum of {@link TrackedUse#getLastUsedInstant()} and
* {@link #getLastBorrowTime()}; otherwise this method gives the same
* value as {@link #getLastBorrowTime()}.
*
* @return the last Instant this object was used.
*/
@Override
public Instant getLastUsedInstant() {
if (object instanceof TrackedUse) {
return PoolImplUtils.max(((TrackedUse) object).getLastUsedInstant(), lastUseInstant);
}
return lastUseInstant;
}
/**
* Gets an estimate of the last time this object was used. If the class
* of the pooled object implements {@link TrackedUse}, what is returned is
* the maximum of {@link TrackedUse#getLastUsedInstant()} and
* {@link #getLastBorrowTime()}; otherwise this method gives the same
* value as {@link #getLastBorrowTime()}.
*
* @return the last time this object was used
*/
@Override
public long getLastUsedTime() {
return getLastUsedInstant().toEpochMilli();
}
@Override
public T getObject() {
return object;
}
/**
* Gets the state of this object.
* @return state
*/
@Override
public synchronized PooledObjectState getState() {
return state;
}
/**
* Sets the state to {@link PooledObjectState#INVALID INVALID}.
*/
@Override
public synchronized void invalidate() {
state = PooledObjectState.INVALID;
}
/**
* Marks the pooled object as {@link PooledObjectState#ABANDONED ABANDONED}.
*/
@Override
public synchronized void markAbandoned() {
state = PooledObjectState.ABANDONED;
}
/**
* Marks the pooled object as {@link PooledObjectState#RETURNING RETURNING}.
*/
@Override
public synchronized void markReturning() {
state = PooledObjectState.RETURNING;
}
/**
* Gets the current instant of the clock.
*
* @return the current instant of the clock.
*/
private Instant now() {
return systemClock.instant();
}
@Override
public void printStackTrace(final PrintWriter writer) {
boolean written = borrowedBy.printStackTrace(writer);
written |= usedBy.printStackTrace(writer);
if (written) {
writer.flush();
}
}
@Override
public void setLogAbandoned(final boolean logAbandoned) {
this.logAbandoned = logAbandoned;
}
/**
* Configures the stack trace generation strategy based on whether or not fully
* detailed stack traces are required. When set to false, abandoned logs may
* only include caller class information rather than method names, line numbers,
* and other normal metadata available in a full stack trace.
*
* @param requireFullStackTrace the new configuration setting for abandoned object
* logging
* @since 2.5
*/
@Override
public void setRequireFullStackTrace(final boolean requireFullStackTrace) {
borrowedBy = CallStackUtils.newCallStack("'Pooled object created' " +
"yyyy-MM-dd HH:mm:ss Z 'by the following code has not been returned to the pool:'",
true, requireFullStackTrace);
usedBy = CallStackUtils.newCallStack("The last code to use this object was:",
false, requireFullStackTrace);
}
@Override
public synchronized boolean startEvictionTest() {
if (state == PooledObjectState.IDLE) {
state = PooledObjectState.EVICTION;
return true;
}
return false;
}
@Override
public String toString() {
final StringBuilder result = new StringBuilder();
result.append("Object: ");
result.append(object.toString());
result.append(", State: ");
synchronized (this) {
result.append(state.toString());
}
return result.toString();
// TODO add other attributes
}
@Override
public void use() {
lastUseInstant = now();
usedBy.fillInStackTrace();
}
}
|