Skip to content

Commit 694b67a

Browse files
committed
DATACOUCH-40 - Make ClusterInfo more reliable on the values returned.
1 parent 47a0125 commit 694b67a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/main/java/org/springframework/data/couchbase/monitor/ClusterInfo.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ public ClusterInfo(final CouchbaseClient client) {
3737

3838
@ManagedMetric(description = "Total RAM assigned")
3939
public long getTotalRAMAssigned() {
40-
return (Long) parseStorageTotals().get("ram").get("total");
40+
return convertPotentialLong(parseStorageTotals().get("ram").get("total"));
4141
}
4242

4343
@ManagedMetric(description = "Total RAM used")
4444
public long getTotalRAMUsed() {
45-
return (Long) parseStorageTotals().get("ram").get("used");
45+
return convertPotentialLong(parseStorageTotals().get("ram").get("used"));
4646
}
4747

4848
@ManagedMetric(description = "Total Disk Space assigned")
4949
public long getTotalDiskAssigned() {
50-
return (Long) parseStorageTotals().get("hdd").get("total");
50+
return convertPotentialLong(parseStorageTotals().get("hdd").get("total"));
5151
}
5252

5353
@ManagedMetric(description = "Total Disk Space used")
5454
public long getTotalDiskUsed() {
55-
return (Long) parseStorageTotals().get("hdd").get("used");
55+
return convertPotentialLong(parseStorageTotals().get("hdd").get("used"));
5656
}
5757

5858
@ManagedMetric(description = "Total Disk Space free")
5959
public long getTotalDiskFree() {
60-
return (Long) parseStorageTotals().get("hdd").get("free");
60+
return convertPotentialLong(parseStorageTotals().get("hdd").get("free"));
6161
}
6262

6363
@ManagedAttribute(description = "Cluster is Balanced")
@@ -75,6 +75,24 @@ public int getMaxBuckets() {
7575
return (Integer) fetchPoolInfo().get("maxBucketCount");
7676
}
7777

78+
/**
79+
* Depending on the value size, either int or long can be passed in and get
80+
* converted to long.
81+
*
82+
* @param value the value to convert.
83+
*
84+
* @return the converted value.
85+
*/
86+
private long convertPotentialLong(Object value) {
87+
if (value instanceof Integer) {
88+
return new Long((Integer) value);
89+
} else if (value instanceof Long) {
90+
return (Long) value;
91+
} else {
92+
throw new IllegalStateException("Cannot convert value to long: " + value);
93+
}
94+
}
95+
7896
private HashMap<String, Object> fetchPoolInfo() {
7997
return getTemplate().getForObject("http://"
8098
+ randomAvailableHostname() + ":8091/pools/default", HashMap.class);

src/test/java/org/springframework/data/couchbase/monitor/ClusterInfoTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void setup() throws Exception {
4949
}
5050

5151
@Test
52-
public void totalRAMAssigned() {
53-
assertThat(ci.getTotalRAMAssigned(), greaterThan(0L));
52+
public void totalDiskAssigned() {
53+
assertThat(ci.getTotalDiskAssigned(), greaterThan(0L));
5454
}
5555

5656
@Test

0 commit comments

Comments
 (0)