Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created May 9, 2011 23:57
Show Gist options
  • Select an option

  • Save ingenthr/963674 to your computer and use it in GitHub Desktop.

Select an option

Save ingenthr/963674 to your computer and use it in GitHub Desktop.
A quick check for a particular key with the KetamaLocator
package com.couchbase.mbinspector;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.ConfigurationException;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.NodeLocator;
import net.spy.memcached.vbucket.config.Bucket;
import net.spy.memcached.vbucket.config.ConfigurationParserJSON;
import net.spy.memcached.vbucket.config.Node;
/**
*
* @author ingenthr
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws URISyntaxException, IOException, ConfigurationException, ParseException {
Main main = new Main();
Logger.getLogger("net.spy.memcached").setLevel(Level.FINEST);
//get the top Logger:
Logger topLogger = java.util.logging.Logger.getLogger("");
// Handler for console (reuse it if it already exists)
Handler consoleHandler = null;
//see if there is already a console handler
for (Handler handler : topLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
//found the console handler
consoleHandler = handler;
break;
}
}
if (consoleHandler == null) {
//there was no console handler found, create a new one
consoleHandler = new ConsoleHandler();
topLogger.addHandler(consoleHandler);
}
//set the console handler to fine:
consoleHandler.setLevel(java.util.logging.Level.INFO);
main.findIt();
}
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
}
public void findIt() throws ParseException, IOException, URISyntaxException, ConfigurationException {
ArrayList<URI> uriList = new ArrayList();
URI fileURI = new URI("http://10.205.9.200:8091/pools");
uriList.add(fileURI);
MemcachedClient mc;
mc = new MemcachedClient(uriList, "default", "", "");
Runnable pinger = new Pinger(mc);
Thread pingerThread = new Thread(pinger);
pingerThread.start();
ConfigurationParserJSON cpj = new ConfigurationParserJSON();
Bucket theConfig = cpj.parseBucket(readFile("/var/tmp/the.json"));
System.err.println(theConfig.getNodes());
for (Node node : theConfig.getNodes()) {
System.err.println("Node: " + node.getHostname());
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
mc.reconfigure(theConfig);
mc.getVersions();
NodeLocator nodeLocator = mc.getNodeLocator();
System.err.println("Matt is " + nodeLocator.getPrimary("matt"));
System.err.println("66.0_p0013197zds is " + nodeLocator.getPrimary("66.0_p0013197zds"));
System.err.println("Steve is " + nodeLocator.getPrimary("steve"));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.err.println("Matt is " + nodeLocator.getPrimary("matt"));
System.err.println("66.0_p0013197zds is " + nodeLocator.getPrimary("66.0_p0013197zds"));
System.err.println("Steve is " + nodeLocator.getPrimary("steve"));
pingerThread.interrupt();
mc.shutdown(10, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment