add stats message

This commit is contained in:
lyr90329 2014-08-13 17:42:14 +08:00
parent 05a95ad014
commit 156ed948b1
2 changed files with 39 additions and 22 deletions

View File

@ -1,8 +1,7 @@
package server;
package com.myself.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
@ -18,25 +17,26 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import client.Client;
import client.ClientMgr;
import com.myself.client.Client;
import com.myself.client.ClientMgr;
import common.RegisterHandler;
public class MemcachedMain {
HashMap<Integer, ClientConfig> m_mapMemcachedClient;
String webServerHost;
String protocolName;
private String webServerHost;
private String protocolName;
public boolean initConfig() {
m_mapMemcachedClient = new HashMap<Integer, ClientConfig>();
File f = new File(System.getProperty("user.dir"));
String path = f.getPath() + File.separator + "bin" + File.separator;
String path = f.getPath() + File.separator;
System.out.println(path);
readClientsXML(path + "client.xml");
try {
Properties properties = new Properties();
properties.load(new FileInputStream(path+"config.properties"));
properties.load(new FileInputStream(path + "config.properties"));
webServerHost = properties.getProperty("webServerHost").toString();
ClientMgr.nCopyNode = Integer.parseInt(properties.getProperty("replicasNum"));
protocolName = properties.getProperty("consistencyProtocol").toString();
@ -47,21 +47,18 @@ public class MemcachedMain {
}else if(protocolName.equals("weak")){
ClientMgr.protocol = ClientMgr.weak;
}else{
System.err.print("consistency protocol input error");
System.err.println("consistency protocol input error");
return false;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public int getMemcachedNumber() {
System.out.print("Please in put R-Memcached ID:");
System.out.print("输入服务编号:");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
return Integer.decode(scanner.next());

View File

@ -1,7 +1,7 @@
package server;
package com.myself.server;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -15,6 +15,8 @@ import messageBody.memcachedmsg.nm_write_2;
import messageBody.requestMsg.nr_Connected_mem_back;
import messageBody.requestMsg.nr_Read;
import messageBody.requestMsg.nr_Read_res;
import messageBody.requestMsg.nr_Stats;
import messageBody.requestMsg.nr_Stats_res;
import messageBody.requestMsg.nr_write;
import messageBody.requestMsg.nr_write_res;
@ -22,8 +24,8 @@ import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.util.internal.ConcurrentHashMap;
import memcached.MemcachedClient;
import client.ClientMgr;
import com.my.memcached.MemcachedClient;
import com.myself.client.ClientMgr;
import common.EMSGID;
@ -154,6 +156,24 @@ public class memSession implements Runnable {
NetMsg msg = (NetMsg) e.getMessage();
switch (msg.getMsgID()) {
case nr_stats: {
nr_Stats msgLite = msg.getMessageLite();
@SuppressWarnings("rawtypes")
Map stats = client.stats();
if (stats != null) {
nr_Stats_res.Builder builder = nr_Stats_res.newBuilder();
builder.setKey("");
builder.setValue(stats.toString());
builder.setTime(msgLite.getTime());
NetMsg send = NetMsg.newMessage();
send.setMessageLite(builder);
send.setMsgID(EMSGID.nr_stats_res);
webServeChannel.write(send);
return;
}
}
break;
case nm_connected: {
nm_Connected msgLite = msg.getMessageLite();
addClientChannel(msgLite.getNum(), e.getChannel());
@ -224,7 +244,7 @@ public class memSession implements Runnable {
return;
}
}
// Can't get data in local Memcached server, try to ask for data from another cache node
// Êý¾ÝÓÐÎÊÌ⣬תÒƸ±±¾
nm_read.Builder builder = nm_read.newBuilder();
builder.setKey(msgLite.getKey());
builder.setTime(msgLite.getTime());
@ -404,7 +424,7 @@ public class memSession implements Runnable {
case nm_write_1_res: {
nm_write_1_res msgLite = msg.getMessageLite();
if (desLockKeyCount(msgLite.getKey()) == ClientMgr.protocol) {
if (desLockKeyCount(msgLite.getKey()) == 0) {
boolean res = client.set(msgLite.getKey(),
msgLite.getValue());
if (res) {