Skip to content

Commit

Permalink
Tweak Xiayang's UID cache hit count by adding a miss counter and rese…
Browse files Browse the repository at this point in the history
…tting

when we hit Long.MAX_VALUE.

Signed-off-by: Chris Larsen <[email protected]>
  • Loading branch information
manolama committed May 21, 2018
1 parent c2a2d9e commit 627620b
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/uid/UniqueId.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public enum UniqueIdType {
private static final short INITIAL_EXP_BACKOFF_DELAY = 800;
/** Maximum number of results to return in suggest(). */
private static final short MAX_SUGGESTIONS = 25;
/** Maximum number of cache_hits. */
private static final long MAX_CACHE_SIZE = 2000000000L;

/** HBase client to use. */
private final HBaseClient client;
Expand Down Expand Up @@ -256,17 +254,29 @@ public long cacheSize() {
}

/**
* Due to the var cache_hits type is int, but the max of int is
* 2147483648, and int happen spilling
* Resets the cache hits counter before rollover. Note that a few updates
* may be dropped due to race conditions at rollover.
*/
private void reNumCache() {
if (cache_hits > MAX_CACHE_SIZE) {
private void incrementCacheHits() {
if (cache_hits >= Long.MAX_VALUE) {
cache_hits = 1;
} else {
cache_hits++;
}
}

/**
* Resets the cache miss counter before rollover. Note that a few updates
* may be dropped due to race conditions at rollover.
*/
private void incrementCacheMiss() {
if (cache_misses >= Long.MAX_VALUE) {
cache_misses = 1;
} else {
cache_misses++;
}
}

/** Returns the number of random UID collisions */
public int randomIdCollisions() {
return random_id_collisions;
Expand Down Expand Up @@ -357,10 +367,10 @@ public Deferred<String> getNameAsync(final byte[] id) {
}
final String name = getNameFromCache(id);
if (name != null) {
reNumCache();
incrementCacheHits();
return Deferred.fromResult(name);
}
cache_misses++;
incrementCacheMiss();
class GetNameCB implements Callback<String, String> {
public String call(final String name) {
if (name == null) {
Expand Down Expand Up @@ -430,10 +440,10 @@ public byte[] getId(final String name) throws NoSuchUniqueName, HBaseException {
public Deferred<byte[]> getIdAsync(final String name) {
final byte[] id = getIdFromCache(name);
if (id != null) {
reNumCache();
incrementCacheHits();
return Deferred.fromResult(id);
}
cache_misses++;
incrementCacheMiss();
class GetIdCB implements Callback<byte[], byte[]> {
public byte[] call(final byte[] id) {
if (id == null) {
Expand Down Expand Up @@ -875,7 +885,7 @@ public Deferred<byte[]> getOrCreateIdAsync(final String name,
// Look in the cache first.
final byte[] id = getIdFromCache(name);
if (id != null) {
reNumCache();
incrementCacheHits();
return Deferred.fromResult(id);
}
// Not found in our cache, so look in HBase instead.
Expand Down

0 comments on commit 627620b

Please sign in to comment.