HBASE-20571 JMXJsonServlet generates invalid JSON if it has NaN in metrics

- CacheStats won't generate NaN metrics.
- JSONBean class will serialize special floating point values as
  "NaN", "Infinity" or "-Infinity"

Signed-off-by: Andrew Purtell <apurtell@apache.org>
This commit is contained in:
Balazs Meszaros 2018-05-11 16:30:38 +02:00 committed by Andrew Purtell
parent 2a309d71cc
commit 2d493556f3
2 changed files with 42 additions and 7 deletions

View File

@ -387,23 +387,53 @@ public class CacheStats {
}
public double getHitRatio() {
return ((float)getHitCount()/(float)getRequestCount());
double requestCount = getRequestCount();
if (requestCount == 0) {
return 0;
}
return getHitCount() / requestCount;
}
public double getHitCachingRatio() {
return ((float)getHitCachingCount()/(float)getRequestCachingCount());
double requestCachingCount = getRequestCachingCount();
if (requestCachingCount == 0) {
return 0;
}
return getHitCachingCount() / requestCachingCount;
}
public double getMissRatio() {
return ((float)getMissCount()/(float)getRequestCount());
double requestCount = getRequestCount();
if (requestCount == 0) {
return 0;
}
return getMissCount() / requestCount;
}
public double getMissCachingRatio() {
return ((float)getMissCachingCount()/(float)getRequestCachingCount());
double requestCachingCount = getRequestCachingCount();
if (requestCachingCount == 0) {
return 0;
}
return getMissCachingCount() / requestCachingCount;
}
public double evictedPerEviction() {
return ((float)getEvictedCount()/(float)getEvictionCount());
double evictionCount = getEvictionCount();
if (evictionCount == 0) {
return 0;
}
return getEvictedCount() / evictionCount;
}
public long getFailedInserts() {

View File

@ -317,7 +317,12 @@ public class JSONBean {
jg.writeEndArray();
} else if(value instanceof Number) {
Number n = (Number)value;
double doubleValue = n.doubleValue();
if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
jg.writeString(n.toString());
} else {
jg.writeNumber(n.toString());
}
} else if(value instanceof Boolean) {
Boolean b = (Boolean)value;
jg.writeBoolean(b);