Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idle time should not depend on metrics recorder #60

Closed
violetagg opened this issue Jul 27, 2019 · 1 comment
Closed

Idle time should not depend on metrics recorder #60

violetagg opened this issue Jul 27, 2019 · 1 comment
Labels
type/bug A general bug warn/api-change Breaking change with compilation errors
Milestone

Comments

@violetagg
Copy link
Member

Expected behavior

Idle time should not depend on metrics recorder

Actual behavior

The current implementation uses metrics recorder to calculate the idle time

void markReleased() {
    if (STATE.compareAndSet(this, STATE_ACQUIRED, STATE_RELEASED)) {
        this.timeSinceRelease = metricsRecorder.now();
    }
}

When there is no metrics recorder reactor.pool.NoOpPoolMetricsRecorder is used
where now() and measureTime(long startTimeMillis) always return 0

This change works for me

diff --git a/src/main/java/reactor/pool/NoOpPoolMetricsRecorder.java b/src/main/java/reactor/pool/NoOpPoolMetricsRecorder.java
index 532b4f8..419f7b6 100644
--- a/src/main/java/reactor/pool/NoOpPoolMetricsRecorder.java
+++ b/src/main/java/reactor/pool/NoOpPoolMetricsRecorder.java
@@ -30,12 +30,7 @@ final class NoOpPoolMetricsRecorder implements PoolMetricsRecorder {
 
     @Override
     public long now() {
-        return 0L;
-    }
-
-    @Override
-    public long measureTime(long startTimeMillis) {
-        return 0;
+        return System.currentTimeMillis();
     }
 
     @Override
diff --git a/src/main/java/reactor/pool/PoolMetricsRecorder.java b/src/main/java/reactor/pool/PoolMetricsRecorder.java
index 437cefb..37bd741 100644
--- a/src/main/java/reactor/pool/PoolMetricsRecorder.java
+++ b/src/main/java/reactor/pool/PoolMetricsRecorder.java
@@ -32,7 +32,9 @@ public interface PoolMetricsRecorder {
 	 * @param startTimeMillis the starting time initially obtained via {@link #now()}
 	 * @return the elapsed time in milliseconds
 	 */
-	long measureTime(long startTimeMillis);
+	default long measureTime(long startTimeMillis) {
+		return now() - startTimeMillis;
+	}
 
 	/**
 	 * Get a starting time with milliseconds resolution.

Steps to reproduce

Reactor Pool version

current snapshot version

JVM version (e.g. java -version)

OS version (e.g. uname -a)

@simonbasle simonbasle added warn/api-change Breaking change with compilation errors type/bug A general bug labels Jul 29, 2019
@simonbasle simonbasle added this to the 0.0.1.M3 milestone Jul 29, 2019
@simonbasle
Copy link
Member

The concern between exporting the metrics and timestamping/measuring a timing should be separated indeed, since the later is used on the PooledRef for stuff like eviction, as you found out.

This is a bit last minute, but I think it is worth the effort to split that concern and have a Clock option on the PoolBuilder for that purpose, along with removing the now() and measureTime methods on PoolMetricsRecorder.

cc @mp911de for r2dbc-pool

simonbasle added a commit that referenced this issue Jul 29, 2019
This commit removes the responsibility of timestamping from the
PoolMetricsRecorder and puts it on a Clock instance that can be passed
to the PoolBuilder.

`now()` and `measureTime(long)` have been removed from the former, and
pool implementations now use the configured Clock instead. The default
clock is `Clock.SystemUTC()` (since only the millis() method is used).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug A general bug warn/api-change Breaking change with compilation errors
Projects
None yet
Development

No branches or pull requests

2 participants