Skip to content

Commit

Permalink
Use the scale in the var name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Gregory committed Feb 2, 2021
1 parent 7a0dffb commit 0da5c54
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 122 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/apache/commons/pool2/PoolUtils.java
Expand Up @@ -1370,7 +1370,7 @@ private static final class ErodingFactor {
private final float factor;

/** Time of next shrink event */
private transient volatile long nextShrink;
private transient volatile long nextShrinkMillis;

/** High water mark - largest numIdle encountered */
private transient volatile int idleHighWaterMark;
Expand All @@ -1383,7 +1383,7 @@ private static final class ErodingFactor {
*/
public ErodingFactor(final float factor) {
this.factor = factor;
nextShrink = System.currentTimeMillis() + (long) (900000 * factor); // now
nextShrinkMillis = System.currentTimeMillis() + (long) (900000 * factor); // now
// +
// 15
// min
Expand All @@ -1395,18 +1395,18 @@ public ErodingFactor(final float factor) {
/**
* Updates internal state using the supplied time and numIdle.
*
* @param now
* @param nowMillis
* current time
* @param numIdle
* number of idle elements in the pool
*/
public void update(final long now, final int numIdle) {
public void update(final long nowMillis, final int numIdle) {
final int idle = Math.max(0, numIdle);
idleHighWaterMark = Math.max(idle, idleHighWaterMark);
final float maxInterval = 15f;
final float minutes = maxInterval +
((1f - maxInterval) / idleHighWaterMark) * idle;
nextShrink = now + (long) (minutes * 60000f * factor);
nextShrinkMillis = nowMillis + (long) (minutes * 60000f * factor);
}

/**
Expand All @@ -1415,7 +1415,7 @@ public void update(final long now, final int numIdle) {
* @return next shrink time
*/
public long getNextShrink() {
return nextShrink;
return nextShrinkMillis;
}

/**
Expand Down Expand Up @@ -1482,16 +1482,16 @@ public T borrowObject() throws Exception, NoSuchElementException,
@Override
public void returnObject(final T obj) {
boolean discard = false;
final long now = System.currentTimeMillis();
final long nowMillis = System.currentTimeMillis();
synchronized (pool) {
if (factor.getNextShrink() < now) { // XXX: Pool 3: move test
if (factor.getNextShrink() < nowMillis) { // XXX: Pool 3: move test
// out of sync block
final int numIdle = pool.getNumIdle();
if (numIdle > 0) {
discard = true;
}

factor.update(now, numIdle);
factor.update(nowMillis, numIdle);
}
}
try {
Expand Down Expand Up @@ -1651,16 +1651,16 @@ public V borrowObject(final K key) throws Exception,
@Override
public void returnObject(final K key, final V obj) throws Exception {
boolean discard = false;
final long now = System.currentTimeMillis();
final long nowMillis = System.currentTimeMillis();
final ErodingFactor factor = getErodingFactor(key);
synchronized (keyedPool) {
if (factor.getNextShrink() < now) {
if (factor.getNextShrink() < nowMillis) {
final int numIdle = getNumIdle(key);
if (numIdle > 0) {
discard = true;
}

factor.update(now, numIdle);
factor.update(nowMillis, numIdle);
}
}
try {
Expand Down
Expand Up @@ -38,10 +38,10 @@ 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 long createTime = System.currentTimeMillis();
private volatile long lastBorrowTime = createTime;
private volatile long lastUseTime = createTime;
private volatile long lastReturnTime = createTime;
private final long createTimeMillis = System.currentTimeMillis();
private volatile long lastBorrowTimeMillis = createTimeMillis;
private volatile long lastUseTimeMillis = createTimeMillis;
private volatile long lastReturnTimeMillis = createTimeMillis;
private volatile boolean logAbandoned = false;
private volatile CallStack borrowedBy = NoOpCallStack.INSTANCE;
private volatile CallStack usedBy = NoOpCallStack.INSTANCE;
Expand All @@ -64,14 +64,14 @@ public T getObject() {

@Override
public long getCreateTime() {
return createTime;
return createTimeMillis;
}

@Override
public long getActiveTimeMillis() {
// Take copies to avoid threading issues
final long rTime = lastReturnTime;
final long bTime = lastBorrowTime;
final long rTime = lastReturnTimeMillis;
final long bTime = lastBorrowTimeMillis;

if (rTime > bTime) {
return rTime - bTime;
Expand All @@ -81,7 +81,7 @@ public long getActiveTimeMillis() {

@Override
public long getIdleTimeMillis() {
final long elapsed = System.currentTimeMillis() - lastReturnTime;
final long elapsed = System.currentTimeMillis() - lastReturnTimeMillis;
// elapsed may be negative if:
// - another thread updates lastReturnTime during the calculation window
// - System.currentTimeMillis() is not monotonic (e.g. system time is set back)
Expand All @@ -90,12 +90,12 @@ public long getIdleTimeMillis() {

@Override
public long getLastBorrowTime() {
return lastBorrowTime;
return lastBorrowTimeMillis;
}

@Override
public long getLastReturnTime() {
return lastReturnTime;
return lastReturnTimeMillis;
}

/**
Expand All @@ -120,9 +120,9 @@ public long getBorrowedCount() {
@Override
public long getLastUsedTime() {
if (object instanceof TrackedUse) {
return Math.max(((TrackedUse) object).getLastUsed(), lastUseTime);
return Math.max(((TrackedUse) object).getLastUsed(), lastUseTimeMillis);
}
return lastUseTime;
return lastUseTimeMillis;
}

@Override
Expand Down Expand Up @@ -187,8 +187,8 @@ public synchronized boolean endEvictionTest(
public synchronized boolean allocate() {
if (state == PooledObjectState.IDLE) {
state = PooledObjectState.ALLOCATED;
lastBorrowTime = System.currentTimeMillis();
lastUseTime = lastBorrowTime;
lastBorrowTimeMillis = System.currentTimeMillis();
lastUseTimeMillis = lastBorrowTimeMillis;
borrowedCount++;
if (logAbandoned) {
borrowedBy.fillInStackTrace();
Expand Down Expand Up @@ -216,7 +216,7 @@ public synchronized boolean deallocate() {
if (state == PooledObjectState.ALLOCATED ||
state == PooledObjectState.RETURNING) {
state = PooledObjectState.IDLE;
lastReturnTime = System.currentTimeMillis();
lastReturnTimeMillis = System.currentTimeMillis();
borrowedBy.clear();
return true;
}
Expand All @@ -234,7 +234,7 @@ public synchronized void invalidate() {

@Override
public void use() {
lastUseTime = System.currentTimeMillis();
lastUseTimeMillis = System.currentTimeMillis();
usedBy.fillInStackTrace();
}

Expand Down
Expand Up @@ -337,7 +337,7 @@ public T borrowObject(final K key, final long borrowMaxWaitMillis) throws Except
final boolean blockWhenExhausted = getBlockWhenExhausted();

boolean create;
final long waitTime = System.currentTimeMillis();
final long waitTimeMillis = System.currentTimeMillis();
final ObjectDeque<T> objectDeque = register(key);

try {
Expand Down Expand Up @@ -420,7 +420,7 @@ public T borrowObject(final K key, final long borrowMaxWaitMillis) throws Except
deregister(key);
}

updateStatsBorrow(p, System.currentTimeMillis() - waitTime);
updateStatsBorrow(p, System.currentTimeMillis() - waitTimeMillis);

return p.getObject();
}
Expand Down
Expand Up @@ -422,7 +422,7 @@ public T borrowObject(final long borrowMaxWaitMillis) throws Exception {
final boolean blockWhenExhausted = getBlockWhenExhausted();

boolean create;
final long waitTime = System.currentTimeMillis();
final long waitTimeMillis = System.currentTimeMillis();

while (p == null) {
create = false;
Expand Down Expand Up @@ -500,7 +500,7 @@ public T borrowObject(final long borrowMaxWaitMillis) throws Exception {
}
}

updateStatsBorrow(p, System.currentTimeMillis() - waitTime);
updateStatsBorrow(p, System.currentTimeMillis() - waitTimeMillis);

return p.getObject();
}
Expand Down Expand Up @@ -1068,16 +1068,16 @@ private int getNumTests() {
@SuppressWarnings("resource") // PrintWriter is managed elsewhere
private void removeAbandoned(final AbandonedConfig abandonedConfig) {
// Generate a list of abandoned objects to remove
final long now = System.currentTimeMillis();
final long timeout =
now - (abandonedConfig.getRemoveAbandonedTimeout() * 1000L);
final long nowMillis = System.currentTimeMillis();
final long timeoutMillis =
nowMillis - (abandonedConfig.getRemoveAbandonedTimeout() * 1000L);
final ArrayList<PooledObject<T>> remove = new ArrayList<>();
final Iterator<PooledObject<T>> it = allObjects.values().iterator();
while (it.hasNext()) {
final PooledObject<T> pooledObject = it.next();
synchronized (pooledObject) {
if (pooledObject.getState() == PooledObjectState.ALLOCATED &&
pooledObject.getLastUsedTime() <= timeout) {
pooledObject.getLastUsedTime() <= timeoutMillis) {
pooledObject.markAbandoned();
remove.add(pooledObject);
}
Expand Down
Expand Up @@ -66,7 +66,7 @@ public boolean printStackTrace(final PrintWriter writer) {
message = messageFormat;
} else {
synchronized (dateFormat) {
message = dateFormat.format(Long.valueOf(snapshotRef.timestamp));
message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
}
}
writer.println(message);
Expand Down Expand Up @@ -109,7 +109,7 @@ private List<WeakReference<Class<?>>> getCallStack() {
* A snapshot of a class stack.
*/
private static class Snapshot {
private final long timestamp = System.currentTimeMillis();
private final long timestampMillis = System.currentTimeMillis();
private final List<WeakReference<Class<?>>> stack;

/**
Expand Down
Expand Up @@ -58,7 +58,7 @@ public synchronized boolean printStackTrace(final PrintWriter writer) {
message = messageFormat;
} else {
synchronized (dateFormat) {
message = dateFormat.format(Long.valueOf(snapshotRef.timestamp));
message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
}
}
writer.println(message);
Expand All @@ -81,6 +81,6 @@ public void clear() {
*/
private static class Snapshot extends Throwable {
private static final long serialVersionUID = 1L;
private final long timestamp = System.currentTimeMillis();
private final long timestampMillis = System.currentTimeMillis();
}
}
Expand Up @@ -74,7 +74,7 @@ private void run() throws Exception {
// 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 startTime = System.currentTimeMillis();
final long startTimeMillis = System.currentTimeMillis();
long testIter = 0;
try {
while (true) {
Expand All @@ -89,7 +89,7 @@ private void run() throws Exception {
}
}
} finally {
System.out.println("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
System.out.println("Time: " + (System.currentTimeMillis() - startTimeMillis) / 1000.0);
service.shutdown();
}
}
Expand Down Expand Up @@ -147,8 +147,8 @@ public Object call() throws Exception {

private void busyWait(final long timeMillis) {
// busy waiting intentionally as a simple thread.sleep fails to reproduce
final long endTime = System.currentTimeMillis() + timeMillis;
while (System.currentTimeMillis() < endTime) {
final long endTimeMillis = System.currentTimeMillis() + timeMillis;
while (System.currentTimeMillis() < endTimeMillis) {
// empty
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/org/apache/commons/pool2/Waiter.java
Expand Up @@ -31,8 +31,8 @@ public class Waiter {
private boolean active = false;
private boolean valid = true;
private long latency = 0;
private long lastPassivated = 0;
private long lastIdleTimeMs = 0;
private long lastPassivatedMillis = 0;
private long lastIdleTimeMillis = 0;
private long passivationCount = 0;
private long validationCount = 0;
private final int id = instanceCount.getAndIncrement();
Expand All @@ -41,7 +41,7 @@ public Waiter(final boolean active, final boolean valid, final long latency) {
this.active = active;
this.valid = valid;
this.latency = latency;
this.lastPassivated = System.currentTimeMillis();
this.lastPassivatedMillis = System.currentTimeMillis();
}

/**
Expand All @@ -65,8 +65,8 @@ public boolean isActive() {
}

/**
* <p>Sets the active state and updates {@link #getLastIdleTimeMs() lastIdleTime}
* or {@link #getLastPassivated() lastPassivated} as appropriate.</p>
* <p>Sets the active state and updates {@link #getLastIdleTimeMillis() lastIdleTime}
* or {@link #getLastPassivatedMillis() lastPassivated} as appropriate.</p>
*
* <p>If the active state is changing from inactive to active, lastIdleTime
* is updated with the current time minus lastPassivated. If the state is
Expand All @@ -86,11 +86,11 @@ public void setActive(final boolean active) {
return;
}
this.active = active;
final long currentTime = System.currentTimeMillis();
final long currentTimeMillis = System.currentTimeMillis();
if (active) { // activating
lastIdleTimeMs = currentTime - lastPassivated;
lastIdleTimeMillis = currentTimeMillis - lastPassivatedMillis;
} else { // passivating
lastPassivated = currentTime;
lastPassivatedMillis = currentTimeMillis;
passivationCount++;
}
}
Expand Down Expand Up @@ -119,22 +119,22 @@ public void setValid(final boolean valid) {
*
* @return time of last passivation
*/
public long getLastPassivated() {
return lastPassivated;
public long getLastPassivatedMillis() {
return lastPassivatedMillis;
}

/**
* <p>Returns the last idle time for this instance in ms.</p>
*
* <p>When an instance is created, and each subsequent time it is passivated,
* the {@link #getLastPassivated() lastPassivated} property is updated with the
* the {@link #getLastPassivatedMillis() lastPassivated} property is updated with the
* current time. When the next activation occurs, {@code lastIdleTime} is
* updated with the elapsed time since passivation.<p>
*
* @return last idle time
*/
public long getLastIdleTimeMs() {
return lastIdleTimeMs;
public long getLastIdleTimeMillis() {
return lastIdleTimeMillis;
}

/**
Expand Down Expand Up @@ -170,8 +170,8 @@ public String toString() {
buff.append("ID = " + id + '\n');
buff.append("valid = " + valid + '\n');
buff.append("active = " + active + '\n');
buff.append("lastPassivated = " + lastPassivated + '\n');
buff.append("lastIdleTimeMs = " + lastIdleTimeMs + '\n');
buff.append("lastPassivated = " + lastPassivatedMillis + '\n');
buff.append("lastIdleTimeMs = " + lastIdleTimeMillis + '\n');
buff.append("latency = " + latency + '\n');
return buff.toString();
}
Expand Down

0 comments on commit 0da5c54

Please sign in to comment.