Skip to content

Commit d38f445

Browse files
committed
Tweak Xiayang's UID cache hit count by adding a miss counter and resetting
when we hit Long.MAX_VALUE. Signed-off-by: Chris Larsen <[email protected]>
1 parent 7551372 commit d38f445

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/uid/UniqueId.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ public enum UniqueIdType {
8383
private static final short INITIAL_EXP_BACKOFF_DELAY = 800;
8484
/** Maximum number of results to return in suggest(). */
8585
private static final short MAX_SUGGESTIONS = 25;
86-
/** Maximum number of cache_hits. */
87-
private static final long MAX_CACHE_SIZE = 2000000000L;
8886

8987
/** HBase client to use. */
9088
private final HBaseClient client;
@@ -211,17 +209,29 @@ public long cacheSize() {
211209
}
212210

213211
/**
214-
* Due to the var cache_hits type is int, but the max of int is
215-
* 2147483648, and int happen spilling
212+
* Resets the cache hits counter before rollover. Note that a few updates
213+
* may be dropped due to race conditions at rollover.
216214
*/
217-
private void reNumCache() {
218-
if (cache_hits > MAX_CACHE_SIZE) {
215+
private void incrementCacheHits() {
216+
if (cache_hits >= Long.MAX_VALUE) {
219217
cache_hits = 1;
220218
} else {
221219
cache_hits++;
222220
}
223221
}
224222

223+
/**
224+
* Resets the cache miss counter before rollover. Note that a few updates
225+
* may be dropped due to race conditions at rollover.
226+
*/
227+
private void incrementCacheMiss() {
228+
if (cache_misses >= Long.MAX_VALUE) {
229+
cache_misses = 1;
230+
} else {
231+
cache_misses++;
232+
}
233+
}
234+
225235
/** Returns the number of random UID collisions */
226236
public int randomIdCollisions() {
227237
return random_id_collisions;
@@ -305,10 +315,10 @@ public Deferred<String> getNameAsync(final byte[] id) {
305315
}
306316
final String name = getNameFromCache(id);
307317
if (name != null) {
308-
reNumCache();
318+
incrementCacheHits();
309319
return Deferred.fromResult(name);
310320
}
311-
cache_misses++;
321+
incrementCacheMiss();
312322
class GetNameCB implements Callback<String, String> {
313323
public String call(final String name) {
314324
if (name == null) {
@@ -360,10 +370,10 @@ public byte[] getId(final String name) throws NoSuchUniqueName, HBaseException {
360370
public Deferred<byte[]> getIdAsync(final String name) {
361371
final byte[] id = getIdFromCache(name);
362372
if (id != null) {
363-
reNumCache();
373+
incrementCacheHits();
364374
return Deferred.fromResult(id);
365375
}
366-
cache_misses++;
376+
incrementCacheMiss();
367377
class GetIdCB implements Callback<byte[], byte[]> {
368378
public byte[] call(final byte[] id) {
369379
if (id == null) {
@@ -787,7 +797,7 @@ public Deferred<byte[]> getOrCreateIdAsync(final String name,
787797
// Look in the cache first.
788798
final byte[] id = getIdFromCache(name);
789799
if (id != null) {
790-
reNumCache();
800+
incrementCacheHits();
791801
return Deferred.fromResult(id);
792802
}
793803
// Not found in our cache, so look in HBase instead.

0 commit comments

Comments
 (0)