adriancole commented on a change in pull request #908: Fixes some edge cases 
around sampler resetting
URL: 
https://github.com/apache/incubator-zipkin-brave/pull/908#discussion_r289582385
 
 

 ##########
 File path: brave/src/main/java/brave/sampler/RateLimitingSampler.java
 ##########
 @@ -55,14 +55,17 @@ public static Sampler create(int tracesPerSecond) {
     long updateAt = nextReset.get();
 
     long nanosUntilReset = -(now - updateAt); // because nanoTime can be 
negative
-    boolean shouldReset = nanosUntilReset <= 0;
-    if (shouldReset) {
-      if (nextReset.compareAndSet(updateAt, updateAt + NANOS_PER_SECOND)) {
-        usage.set(0);
-      }
+    if (nanosUntilReset <= 0) {
+      // Attempt to move into the next sampling interval.
+      // nanosUntilReset is now invalid regardless of race winner, so we can't 
sample based on it.
+      if (nextReset.compareAndSet(updateAt, updateAt + NANOS_PER_SECOND)) 
usage.set(0);
 
 Review comment:
   I think maybe the below is the easier way to get to the point (we should be 
working off "now"), I will also add a unit test so recursion worries float away.
   
   ```diff
   --- a/brave/src/main/java/brave/sampler/RateLimitingSampler.java
   +++ b/brave/src/main/java/brave/sampler/RateLimitingSampler.java
   @@ -74,7 +74,7 @@ public class RateLimitingSampler extends Sampler {
        if (nanosUntilReset <= 0) {
          // Attempt to move into the next sampling interval.
          // nanosUntilReset is now invalid regardless of race winner, so we 
can't sample based on it.
   -      if (nextReset.compareAndSet(updateAt, updateAt + NANOS_PER_SECOND)) 
usage.set(0);
   +      if (nextReset.compareAndSet(updateAt, now + NANOS_PER_SECOND)) 
usage.set(0);
    
          // recurse as it is simpler than resetting all the locals.
          // reset happens once per second, this code doesn't take a second, so 
no infinite recursion.
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to