-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement live internal latency tracker (#651)
* Implement live internal latency tracker * Rename to accumulator * Refactor * Refactor * Add tracker impl * Delete pubisher function and add test * Delete unused imports * Fix flaky test * Add test for concurrent external work
- Loading branch information
Showing
15 changed files
with
719 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package graphql.nadel | ||
|
||
class NadelExecutionParams internal constructor(val nadelExecutionHints: NadelExecutionHints) | ||
import graphql.nadel.time.NadelInternalLatencyTracker | ||
|
||
class NadelExecutionParams internal constructor( | ||
val executionHints: NadelExecutionHints, | ||
val latencyTracker: NadelInternalLatencyTracker, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
lib/src/main/java/graphql/nadel/time/NadelInternalLatencyTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package graphql.nadel.time | ||
|
||
import java.time.Duration | ||
|
||
interface NadelInternalLatencyTracker { | ||
/** | ||
* Gets the _current_ internal latency. | ||
* | ||
* This can be invoked before the latency is completely tracked to get a running track | ||
* of latency. | ||
*/ | ||
fun getInternalLatency(): Duration | ||
} | ||
|
69 changes: 69 additions & 0 deletions
69
lib/src/main/java/graphql/nadel/time/NadelInternalLatencyTrackerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package graphql.nadel.time | ||
|
||
import java.time.Duration | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import java.util.function.Supplier | ||
|
||
open class NadelInternalLatencyTrackerImpl( | ||
/** | ||
* Stopwatch to track internal latency. | ||
*/ | ||
private val internalLatency: NadelStopwatch, | ||
) : NadelInternalLatencyTracker { | ||
private val outstandingExternalLatencyCount = AtomicInteger() | ||
|
||
override fun getInternalLatency(): Duration { | ||
return internalLatency.elapsed() | ||
} | ||
|
||
fun onExternalRun(code: Runnable) { | ||
onExternalCallStart() | ||
|
||
try { | ||
code.run() | ||
} finally { | ||
onExternalCallEnd() | ||
} | ||
} | ||
|
||
fun <T : Any> onExternalGet(code: Supplier<T>): T { | ||
onExternalCallStart() | ||
|
||
try { | ||
return code.get() | ||
} finally { | ||
onExternalCallEnd() | ||
} | ||
} | ||
|
||
fun <T : Any> onExternalFuture(future: CompletableFuture<T>): CompletableFuture<T> { | ||
onExternalCallStart() | ||
|
||
return future | ||
.whenComplete { _, _ -> | ||
onExternalCallEnd() | ||
} | ||
} | ||
|
||
fun <T : Any> onExternalFuture(future: Supplier<CompletableFuture<T>>): CompletableFuture<T> { | ||
onExternalCallStart() | ||
|
||
return future.get() | ||
.whenComplete { _, _ -> | ||
onExternalCallEnd() | ||
} | ||
} | ||
|
||
protected fun onExternalCallStart() { | ||
if (outstandingExternalLatencyCount.getAndIncrement() == 0) { | ||
internalLatency.stop() | ||
} | ||
} | ||
|
||
protected fun onExternalCallEnd() { | ||
if (outstandingExternalLatencyCount.decrementAndGet() == 0) { | ||
internalLatency.start() | ||
} | ||
} | ||
} |
Oops, something went wrong.