OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
7 * | 7 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 9 * |
10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
(...skipping 12 matching lines...) Expand all Loading... |
23 * so that (say) processes do not get synchronized on time inadvertently, e.g.,
a heartbeat task | 23 * so that (say) processes do not get synchronized on time inadvertently, e.g.,
a heartbeat task |
24 * that sends a message every few minutes is smeared so that all clients do not
end up sending a | 24 * that sends a message every few minutes is smeared so that all clients do not
end up sending a |
25 * message at the same time. In particular, given a {@code delay}, returns a val
ue that is randomly | 25 * message at the same time. In particular, given a {@code delay}, returns a val
ue that is randomly |
26 * distributed between [delay - smearPercent * delay, delay + smearPercent * del
ay] | 26 * distributed between [delay - smearPercent * delay, delay + smearPercent * del
ay] |
27 * | 27 * |
28 */ | 28 */ |
29 public class Smearer { | 29 public class Smearer { |
30 | 30 |
31 private final Random random; | 31 private final Random random; |
32 | 32 |
33 /** The percentage (0, 1.0] for smearing the delay. */ | 33 /** The percentage [0, 1.0] for smearing the delay. */ |
34 private double smearFraction; | 34 private double smearFraction; |
35 | 35 |
36 /** | 36 /** |
37 * Creates a smearer with the given random number generator. If {@code smearPe
rcent} is 0, uses an | 37 * Creates a smearer with the given random number generator. |
38 * internal default for smearing. | |
39 * <p> | 38 * <p> |
40 * REQUIRES: 0 < smearPercent <= 100 | 39 * REQUIRES: 0 < smearPercent <= 100 |
41 */ | 40 */ |
42 public Smearer(Random random, final int smearPercent) { | 41 public Smearer(Random random, final int smearPercent) { |
43 Preconditions.checkState((smearPercent >= 0) && (smearPercent <= 100)); | 42 Preconditions.checkState((smearPercent >= 0) && (smearPercent <= 100)); |
44 this.random = random; | 43 this.random = random; |
45 this.smearFraction = smearPercent / 100.0; | 44 this.smearFraction = smearPercent / 100.0; |
46 } | 45 } |
47 | 46 |
48 /** | 47 /** |
49 * Given a {@code delay}, returns a value that is randomly distributed between | 48 * Given a {@code delay}, returns a value that is randomly distributed between |
50 * [delay - smearPercent * delay, delay + smearPercent * delay] | 49 * [delay - smearPercent * delay, delay + smearPercent * delay] |
51 */ | 50 */ |
52 public int getSmearedDelay(int delay) { | 51 public int getSmearedDelay(int delay) { |
53 // Get a random number between -1 and 1 and then multiply that by the smear | 52 // Get a random number between -1 and 1 and then multiply that by the smear |
54 // fraction. | 53 // fraction. |
55 double smearFactor = (2 * random.nextDouble() - 1.0) * smearFraction; | 54 double smearFactor = (2 * random.nextDouble() - 1.0) * smearFraction; |
56 return (int) Math.ceil(delay + delay * smearFactor); | 55 return (int) Math.ceil(delay + delay * smearFactor); |
57 } | 56 } |
58 } | 57 } |
OLD | NEW |