-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for Cassandra 3.x version (#1)
- Loading branch information
1 parent
49a34e9
commit feb4b1b
Showing
7 changed files
with
286 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/pixonic/ctop/metrics/AbstractMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.pixonic.ctop.metrics; | ||
|
||
import com.pixonic.ctop.StringUtils; | ||
import com.pixonic.ctop.SttySupport; | ||
|
||
import javax.management.MBeanServerConnection; | ||
import java.util.Date; | ||
import java.util.Iterator; | ||
import java.util.NavigableSet; | ||
|
||
public abstract class AbstractMetrics implements Metrics { | ||
|
||
protected volatile boolean shutdown = false; | ||
protected final long interval; | ||
protected final MBeanServerConnection remote; | ||
protected final String keySpace; | ||
|
||
AbstractMetrics(long interval, MBeanServerConnection remote, String keySpace) { | ||
this.interval = interval; | ||
this.remote = remote; | ||
this.keySpace = keySpace; | ||
} | ||
|
||
void printMetrics(NavigableSet<ResultItem> readResult, NavigableSet<ResultItem> writeResult) { | ||
//clear console | ||
System.out.print("\033[H\033[2J"); | ||
System.out.flush(); | ||
|
||
System.out.println("Cassandra top v0.2"); | ||
System.out.println(); | ||
System.out.println(new Date() + " / " + interval + "s"); | ||
System.out.println(); | ||
|
||
int width = SttySupport.getTerminalWidth(); | ||
int height = SttySupport.getTerminalHeight(); | ||
|
||
int posWrite = width / 2; | ||
String leftStr = "Reads", rightStr = "Writes"; | ||
|
||
System.out.println(makeLine(leftStr, rightStr, posWrite)); | ||
System.out.println(); | ||
Iterator<ResultItem> readIt = readResult.iterator(); | ||
Iterator<ResultItem> writeIt = writeResult.iterator(); | ||
Long maxReadCount = null, maxWriteCount = null; | ||
for(int i = 7; i < height; i++) { | ||
if (readIt.hasNext()) { | ||
ResultItem resultItem = readIt.next(); | ||
if (maxReadCount == null) maxReadCount = resultItem.count; | ||
leftStr = formatCounter(resultItem, maxReadCount); | ||
} else { | ||
leftStr = ""; | ||
} | ||
if (writeIt.hasNext()) { | ||
ResultItem resultItem = writeIt.next(); | ||
if (maxWriteCount == null) maxWriteCount = resultItem.count; | ||
rightStr = formatCounter(resultItem, maxWriteCount); | ||
} else { | ||
rightStr = ""; | ||
} | ||
if (leftStr.length() == 0 && rightStr.length() == 0) break; | ||
System.out.println(makeLine(leftStr, rightStr, posWrite)); | ||
} | ||
} | ||
|
||
@Override public void shutdown() { | ||
shutdown = true; | ||
} | ||
|
||
protected String formatCounter(ResultItem resultItem, long maxCount) { | ||
int maxLen = String.valueOf(maxCount).length(); | ||
return StringUtils.leftPad(String.valueOf(resultItem.count), maxLen + 1) + " " + resultItem; | ||
} | ||
|
||
protected String makeLine(String left, String right, int rightPos) { | ||
if (left.length() > rightPos) left = left.substring(0, rightPos); | ||
if (right.length() > rightPos) left = right.substring(0, rightPos); | ||
return StringUtils.rightPad(left, rightPos) + " " + right; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/pixonic/ctop/metrics/CurrentMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.pixonic.ctop.metrics; | ||
|
||
import javax.management.*; | ||
import java.io.IOException; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.NavigableSet; | ||
import java.util.TreeSet; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CurrentMetrics extends AbstractMetrics { | ||
|
||
private static final String KEY_PROPERTY = "scope"; | ||
private static final String ATTRIBUTE = "Count"; | ||
|
||
CurrentMetrics(long interval, MBeanServerConnection remote, String keySpace) { | ||
super(interval, remote, keySpace); | ||
} | ||
|
||
@Override public void printMetrics() throws Exception { | ||
ObjectName readObjectName = new ObjectName("org.apache.cassandra.metrics:type=Table,keyspace=" + keySpace + ",scope=*,name=ReadLatency"); | ||
ObjectName writeObjectName = new ObjectName("org.apache.cassandra.metrics:type=Table,keyspace=" + keySpace + ",scope=*,name=WriteLatency"); | ||
|
||
List<MonitoringEntry> readItems = getMonitoringEntryList(remote, readObjectName); | ||
List<MonitoringEntry> writeItems = getMonitoringEntryList(remote, writeObjectName); | ||
|
||
while (!shutdown) { | ||
Thread.sleep(TimeUnit.SECONDS.toMillis(interval)); | ||
super.printMetrics(createResultItems(readItems), createResultItems(writeItems)); | ||
} | ||
} | ||
|
||
private NavigableSet<ResultItem> createResultItems(List<MonitoringEntry> monitoringItems) throws AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException, IOException { | ||
NavigableSet<ResultItem> resultItems = new TreeSet<>(); | ||
|
||
for (MonitoringEntry item : monitoringItems) { | ||
MonitoringEntry monitoringEntry = calculateDifference(remote, item); | ||
if (monitoringEntry.count > 0) resultItems.add(new ResultItem(item.objectName, KEY_PROPERTY, monitoringEntry.count)); | ||
} | ||
|
||
return resultItems; | ||
} | ||
|
||
private MonitoringEntry calculateDifference(MBeanServerConnection remote, MonitoringEntry item) throws AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException, IOException { | ||
Long count = (Long) remote.getAttribute(item.objectName, ATTRIBUTE); | ||
|
||
long diff = count - item.count; | ||
|
||
item.count = count; | ||
return new MonitoringEntry(item.objectName, diff); | ||
} | ||
|
||
private List<MonitoringEntry> getMonitoringEntryList(MBeanServerConnection remote, ObjectName objectName) throws IOException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException { | ||
List<MonitoringEntry> monitoringList = new LinkedList<>(); | ||
|
||
for (ObjectName mbean : remote.queryNames(objectName, null)) { | ||
Long count = (Long) remote.getAttribute(mbean, ATTRIBUTE); | ||
if (count > 0) monitoringList.add(new MonitoringEntry(mbean, count)); | ||
} | ||
|
||
return monitoringList; | ||
} | ||
|
||
private static class MonitoringEntry { | ||
private ObjectName objectName; | ||
private long count; | ||
|
||
MonitoringEntry(ObjectName objectName, long count) { | ||
this.objectName = objectName; | ||
this.count = count; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.pixonic.ctop.metrics; | ||
|
||
public interface Metrics { | ||
|
||
void printMetrics() throws Exception; | ||
|
||
void shutdown(); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/pixonic/ctop/metrics/MetricsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.pixonic.ctop.metrics; | ||
|
||
import javax.management.MBeanServerConnection; | ||
|
||
public class MetricsFactory { | ||
|
||
public static Metrics getMetrics(int version, long interval, MBeanServerConnection remote, String keySpace) { | ||
if (version >= 3) { | ||
return new CurrentMetrics(interval, remote, keySpace); | ||
} else { | ||
return new OldMetrics(interval, remote, keySpace); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.