-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JMX client who can fetch tokens from the ring
Change-Id: I3cad0fae9b0fa90f30dcb426ac847c06ebb75e92
- Loading branch information
Radovan Zvoncek
committed
Nov 28, 2014
1 parent
3b1eed2
commit 297ab0d
Showing
5 changed files
with
128 additions
and
0 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
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,16 @@ | ||
package com.spotify.reaper; | ||
|
||
public class ReaperException extends Exception { | ||
|
||
public ReaperException(String s) { | ||
super(s); | ||
} | ||
|
||
public ReaperException(Exception e) { | ||
super(e); | ||
} | ||
|
||
public ReaperException(String s, Exception e) { | ||
super(s,e); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/spotify/reaper/cassandra/ClusterInfo.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,30 @@ | ||
package com.spotify.reaper.cassandra; | ||
|
||
import com.spotify.reaper.ReaperException; | ||
|
||
import java.util.List; | ||
|
||
public class ClusterInfo implements IClusterInfo { | ||
|
||
private String seedHost; | ||
private int seedPort = 0; | ||
|
||
private List<String> tokens; | ||
|
||
public ClusterInfo(String seedHost, int seedPort) { | ||
this.seedHost = seedHost; | ||
this.seedPort = seedPort; | ||
} | ||
|
||
public void init() throws ReaperException { | ||
JMXProxy jmx = seedPort == 0 ? JMXProxy.connect(seedHost) : JMXProxy.connect(seedHost, seedPort); | ||
tokens = jmx.getTokens(); | ||
jmx.close(); | ||
} | ||
|
||
@Override | ||
public List<String> getTokens() { | ||
return tokens; | ||
} | ||
|
||
} |
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.spotify.reaper.cassandra; | ||
|
||
import java.util.List; | ||
|
||
public interface IClusterInfo { | ||
|
||
public List<String> getTokens(); | ||
|
||
} |
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,67 @@ | ||
package com.spotify.reaper.cassandra; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.spotify.reaper.ReaperException; | ||
import org.apache.cassandra.service.StorageServiceMBean; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.util.List; | ||
|
||
import javax.management.JMX; | ||
import javax.management.MBeanServerConnection; | ||
import javax.management.MalformedObjectNameException; | ||
import javax.management.ObjectName; | ||
import javax.management.remote.JMXConnector; | ||
import javax.management.remote.JMXConnectorFactory; | ||
import javax.management.remote.JMXServiceURL; | ||
|
||
public class JMXProxy { | ||
|
||
private static final int DEFAULT_JMX_PORT = 7199; | ||
|
||
private JMXConnector jmxc = null; | ||
private StorageServiceMBean ssProxy; | ||
|
||
public JMXProxy(JMXConnector jmxc, StorageServiceMBean ssProxy) { | ||
this.jmxc = jmxc; | ||
this.ssProxy = ssProxy; | ||
} | ||
|
||
public static JMXProxy connect(String host) throws ReaperException { | ||
return connect(host, DEFAULT_JMX_PORT); | ||
} | ||
|
||
public static JMXProxy connect(String host, int port) throws ReaperException { | ||
JMXServiceURL jmxUrl; | ||
ObjectName name; | ||
try { | ||
jmxUrl = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", | ||
host, port)); | ||
name = new ObjectName("org.apache.cassandra.db:type=StorageService"); | ||
} catch (MalformedURLException | MalformedObjectNameException e) { | ||
throw new ReaperException("Failure during preparations for JMX connection", e); | ||
} | ||
try { | ||
JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl); | ||
MBeanServerConnection mbeanServerConn = jmxc.getMBeanServerConnection(); | ||
StorageServiceMBean | ||
ssProxy = JMX.newMBeanProxy(mbeanServerConn, name, StorageServiceMBean.class); | ||
return new JMXProxy(jmxc, ssProxy); | ||
} catch (IOException e) { | ||
throw new ReaperException("Failure when establishing JMX connection", e); | ||
} | ||
} | ||
|
||
public List<String> getTokens() { | ||
return Lists.newArrayList(ssProxy.getTokenToEndpointMap().keySet()); | ||
} | ||
|
||
public void close() throws ReaperException { | ||
try { | ||
jmxc.close(); | ||
} catch (IOException e) { | ||
throw new ReaperException(e); | ||
} | ||
} | ||
} |