1. add mysql DB in the project
2. fix *server.java: connect db when memcache doesn't contain the request
data
3. fix memcached.jar to get node stats
This commit is contained in:
martji 2014-09-01 12:48:28 +08:00
parent ee537272e0
commit 60aabd4747
16 changed files with 547 additions and 198 deletions

View File

@ -19,6 +19,5 @@
<attribute name="owner.project.facets" value="jst.web.jstl"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/webserver.jar"/>
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>

Binary file not shown.

Binary file not shown.

5
Website/dbcfg.properties Normal file
View File

@ -0,0 +1,5 @@
url = jdbc:mysql://192.168.3.218:3306/tpcw
username = root
password = 1234
sqlget = select c_id, c_uname, c_passwd, c_birthdate from customer where c_id = ?
sqlset = replace into customer(c_id, c_birthdaye) values(?, ?)

View File

@ -1,41 +1,86 @@
/**
* Copyright (c) 2008 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author Greg Whalin <greg@meetup.com>
* @author martji magq@act.buaa.edu.cn
*/
package dao;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.meetup.memcached.*;
import com.meetup.memcaheded.db.DatabaseCon;
import com.myself.server.webSession;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONObject;
public class MemcachedServer {
static String ORIGINPATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
private static String[] serverlist = { "192.168.3.224:20001", "192.168.3.204:20001",
"192.168.3.244:20001", "192.168.3.218:20001" };
@SuppressWarnings("rawtypes")
private static Map oldStats = new HashMap<String, Map>();
public static JSONArray nodeStats = new JSONArray();
private static int threadCount = 0;
private static int startIndex = 0;
public static long time = 0;
private static JSONArray results = new JSONArray();
public static Boolean status = false;
public static void main(String[] args){
MemcachedServer.run(args);
public static void main(String[] args) {
MemcachedServer.run(new String[]{"10","1000","1000","64","0.8"});
}
public static void run(String[] args)
{
PropertyConfigurator.configure(ORIGINPATH +"log4j.properties");//¼ÓÔØ.propertiesÎļþ
String[] serverlist = { "192.168.3.224:20000", "192.168.3.204:20000" , "192.168.3.244:20000" , "192.168.3.218:20000" };
public static void run(String[] args) {
init();
oldStats = getStats();
int threads = Integer.parseInt(args[0]);
int runs = Integer.parseInt(args[1]);
int Nums = Integer.parseInt(args[2]); // how many kilobytes
int size = Integer.parseInt(args[3]); // how many kilobytes
double rate = Double.parseDouble(args[4]); // Áд±ÈÀý
threadCount = threads;
// get object to store
byte[] obj = new byte[size];
for (int i = 0; i < size; i++) {
obj[i] = '1';
}
String value = new String(obj);
String[] keys = new String[Nums];
for (int i = 0; i < Nums; i++) {
keys[i] = "" + i;
}
for (int i = 0; i < threads; i++) {
bench b = new bench(runs, Nums, i, value, keys, rate);
b.start();
}
while (threadCount != 0){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time = time/threads;
getCurrentStats();
System.out.println(nodeStats);
System.out.println("requestNumber: " + results.length());
recycle();
status = true;
}
@SuppressWarnings("rawtypes")
private static void init(){
PropertyConfigurator.configure(ORIGINPATH + "log4j.properties");// ¼ÓÔØ.propertiesÎļþ
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.getInstance();
pool.setServers( serverlist );
pool.setServers(serverlist);
pool.setInitConn(5);
pool.setMinConn(5);
@ -44,130 +89,138 @@ public class MemcachedServer {
pool.setNagle(false);
pool.initialize();
int threads = Integer.parseInt(args[0]);
int runs = Integer.parseInt(args[1]);
int Nums = Integer.parseInt(args[2]); // how many kilobytes
int size = Integer.parseInt(args[3]); // how many kilobytes
double rate = Double.parseDouble(args[4]); //Áд±ÈÀý
// get object to store
byte[] obj = new byte[size];
for (int i = 0; i < size; i++)
{
obj[i] = '1';
}
String value = new String(obj);
String[] keys = new String[Nums];
for (int i = 0; i < Nums; i++)
{
keys[i] = "" + i;
}
// DatabaseCon.getInstance().start();
// for (int i = 0; i < 100000; i++)
// {
// DatabaseCon.getInstance().setKey(String.valueOf(i), value);
// }
// System.out.println("insert data successful");
for (int i = 0; i < threads; i++)
{
bench b = new bench(runs, Nums, i, value, keys, rate);
b.start();
DatabaseCon.getInstance().start();
startIndex = 0;
oldStats = new HashMap<String, Map>();
results = new JSONArray();
status = false;
}
private static void recycle(){
SockIOPool pool = SockIOPool.getInstance();
pool.shutDown();
}
@SuppressWarnings("rawtypes")
public static Map getStats(){
MemcachedClient mc = new MemcachedClient();
Map statsMap = mc.stats(serverlist);
return statsMap;
}
@SuppressWarnings("rawtypes")
public static void getCurrentStats(){
Map newStats = getStats();
if (newStats.size() == 4 && newStats.size() == 4){
for (int i = 0; i < serverlist.length; i++){
Map newNodeStats = (Map) newStats.get(serverlist[i]);
Map oldNodeStats = (Map) oldStats.get(serverlist[i]);
int cmd_get_old = Integer.parseInt((String) oldNodeStats.get("cmd_get"));
int cmd_set_old = Integer.parseInt((String) oldNodeStats.get("cmd_set"));
int cmd_get_new = Integer.parseInt((String) newNodeStats.get("cmd_get"));
int cmd_set_new = Integer.parseInt((String) newNodeStats.get("cmd_set"));
int cmd_get = cmd_get_new - cmd_get_old;
int cmd_set = cmd_set_new - cmd_set_old;
Map<String, Integer> node = new HashMap<>();
node.put("node", i);
node.put("get", cmd_get);
node.put("set", cmd_set);
nodeStats.put(node);
}
}
// pool.shutDown();
// System.exit(1);
}
public static JSONArray getResult() {
JSONArray results = new JSONArray();
JSONArray arrays = webSession.results;
int endIndex = arrays.length();
for (int i = startIndex; i < endIndex && i < startIndex + 1000; i++){
try {
JSONObject aResult = (JSONObject) arrays.get(i);
results.put(aResult);
} catch (Exception e) {
e.printStackTrace();
}
}
startIndex = endIndex < startIndex+1000 ? endIndex : startIndex+1000;
return results;
}
/**
* Test code per thread.
/**
* Test code per thread.
*/
private static class bench extends Thread
{
private static class bench extends Thread {
private int runs;
private String object;
private String[] keys;
private int nums;
@SuppressWarnings("unused")
private int ticks=0;
private int ticks = 0;
@SuppressWarnings("unused")
private long diffTime = 0;
private double rate;
public bench(int runs,int nums, int threadNum, String object, String[] keys, double rate)
{
public bench(int runs, int nums, int threadNum, String object,
String[] keys, double rate) {
this.runs = runs;
this.object = object;
this.object = "(XXX, xxx, 2000-01-01)";
this.keys = keys;
this.nums = nums;
this.rate = rate;
}
public void run()
{
public void run() {
MemcachedClient mc = new MemcachedClient();
mc.setCompressEnable(false);
mc.setCompressThreshold(0);
//DatabaseCon.getInstance().start();
long time = 0;
time = System.nanoTime();
randReadWrite(mc, rate);
randReadWrite(mc, rate);
threadCount --;
time = System.nanoTime() - time;
System.out.println(time/1000000000.0);
MemcachedServer.time += time;
System.err.println(time / 1000000000.0);
}
public void randReadWrite(MemcachedClient mc, double scale)
{
Random randNum = new Random();
for (int i = 1; i <= runs; i++)
{
// long begin = System.nanoTime();
if (Math.random()<scale)
{
mc.get(keys[randNum.nextInt(nums)]);
//System.out.println(var);
public void randReadWrite(MemcachedClient mc, double scale) {
final Random randNum = new Random();
int getCount = (int) (runs*scale);
int setCount = runs - getCount;
for (int i = 0, j = 0, k = 0; i < runs; i++) {
Map<String, String> aResult = new HashMap<String, String>();
if ((Math.random() < scale || j >= setCount) && k < getCount) {
String keyword = keys[randNum.nextInt(nums)];
aResult.put("type", "GET");
aResult.put("key", keyword);
Object out = mc.get(keyword);
if (out == null){
String value = DatabaseCon.getInstance().queryKey(keyword);
if (value == null){
value = object;
}
mc.set(keyword, value);
aResult.put("value", value);
}
else {
aResult.put("value", (String) out);
}
k ++;
} else {
String keyword = keys[randNum.nextInt(nums)];
mc.set(keyword, object);
aResult.put("type", "SET");
aResult.put("key", keyword);
aResult.put("value", object);
j ++;
}
else
{
mc.set(keys[randNum.nextInt(nums)], object);
// DatabaseCon.getInstance().setKey(keys[randNum.nextInt(nums)], object);
}
// diffTime +=System.nanoTime()-begin;
// if (i%1000==0)
// {
// System.out.println(diffTime/1000000000.0);
// diffTime = 0;
// }
try
{
results.put(aResult);
try {
Thread.sleep(0);
} catch (InterruptedException e)
{
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// public synchronized static void method()
// {
// if (ticks == 0)
// {
// diffTime = System.currentTimeMillis();
// }
// ticks++;
// if (ticks == 5000)
// {
// System.out.println(5000.0*1000/(System.currentTimeMillis()-diffTime));
// ticks = 0;
// }
// }
}
}
}

View File

@ -1,3 +1,6 @@
/**
* @author martji magq@act.buaa.edu.cn
*/
package dao;
import java.io.File;
@ -19,6 +22,11 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.myself.database.DatabaseCon;
import com.myself.memcached.MemcachedMgr;
import com.myself.server.ClientConfig;
import com.myself.server.Server;
@ -28,9 +36,10 @@ import common.RegisterHandler;
public class RMemcachedServer {
static HashMap<Integer, ClientConfig> m_mapMemcachedClient;
static String ORIGINPATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
static String ORIGINPATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
public static Boolean status = false;
public static int threadCount = 0;
public static int requestCount = 0;
private static int startIndex = 0;
public static boolean initFlag = false;
@ -38,56 +47,33 @@ public class RMemcachedServer {
private static Map<Integer, String> localStats = new HashMap<>();
public static JSONArray nodeStats = new JSONArray();
public static void main(String[] args){
new Thread(new Runnable() {
public void run() {
RMemcachedServer.run(new String[]{"10","1000","1000","8","0.8"});
}
}).start();
while (!RMemcachedServer.initFlag) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (!RMemcachedServer.status) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
RMemcachedServer.getResult();
}
RMemcachedServer.getResult();
System.out.println(webSession.results.length());
//System.out.println(nodeStats);
//System.out.println(time);
public static void main(String[] args){
// for (int i = 0; i < 1; i++){
// new Thread(new Runnable() {
// public void run() {
// RMemcachedServer.run(new String[]{"10","1000","1000","64","0"});
// }
// }).start();
// while (!RMemcachedServer.initFlag) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// while (!RMemcachedServer.status) {
// RMemcachedServer.getResult();
// try {
// Thread.sleep(200);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// RMemcachedServer.getResult();
// //System.out.println(webSession.results.length());
// }
for (int i = 0; i < 50; i++){
new Thread(new Runnable() {
public void run() {
RMemcachedServer.run(new String[]{"10","1000","1000","8","0.8"});
}
}).start();
while (!RMemcachedServer.initFlag) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (!RMemcachedServer.status) {
RMemcachedServer.getResult();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
RMemcachedServer.getResult();
System.out.println(webSession.results.length());
}
RMemcachedServer.run(new String[]{"10","1000","1000","64","0.8"});
System.exit(0);
}
@ -99,19 +85,19 @@ public class RMemcachedServer {
RMemcachedServer.nodeStats = new JSONArray();
RMemcachedServer.localStats = new HashMap<>();
RMemcachedServer.threadCount = 0;
RMemcachedServer.requestCount = 0;
RMemcachedServer.localStats = null;
webSession.results = new JSONArray();
}
public static void run(String[] args) {
initial();
//System.out.println(ORIGINPATH);
PropertyConfigurator.configure(ORIGINPATH + "log4j.properties");
initConfig();
RegisterHandler.initHandler();
webSession.getInstance().start();
//new webSession().start();
webSession.getInstance().start();
MemcachedMgr clientMgr = MemcachedMgr.getInstance();
clientMgr.init(m_mapMemcachedClient);
@ -119,20 +105,13 @@ public class RMemcachedServer {
Server requestServer = Server.getInstance();
requestServer.init(8888);
localStats = new HashMap<>();
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
getStats();
int threads = Integer.parseInt(args[0]);
int runs = Integer.parseInt(args[1]);
int Nums = Integer.parseInt(args[2]);
int size = Integer.parseInt(args[3]);
double rate = Double.parseDouble(args[4]);
RMemcachedServer.threadCount = threads;
RMemcachedServer.requestCount = runs*threads;
byte[] obj = new byte[size];
for (int i = 0; i < size; i++) {
@ -143,41 +122,43 @@ public class RMemcachedServer {
for (int i = 0; i < Nums; i++) {
keys[i] = "" + i;
}
getStats();
DatabaseCon.getInstance().start();
RMemcachedServer.initFlag = true;
for (int i = 0; i < threads; i++) {
bench b = new bench(runs, Nums, i, value, keys, rate);
b.start();
}
while (threadCount != 0 || startIndex < threads*runs*rate){
//System.out.println("threadCounts= " + threadCount + " startIndex= " + startIndex);
while (WebServerMain.threadCount != 0){
try {
Thread.sleep(200);
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
webSession.getInstance().stats();
while (webSession.stats.size() != 4 || webSession.results.length() < requestCount){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getStats();
System.out.println(nodeStats);
time = time/threads;
status = true;
requestServer.stop();
RMemcachedServer.initFlag = false;
System.out.println("requestNumber: " + requestCount + " resultNumber: " + webSession.results.length());
webSession.session = null;
System.out.println("session finish");
}
public static JSONArray test(){
Map<String, String> stats = new HashMap<>();
stats.put("key", "371");
stats.put("node", "3");
stats.put("type", "GET");
stats.put("value", "111111111111");
JSONArray jsons = new JSONArray();
jsons.put(stats);
jsons.put(stats);
return jsons;
}
public static JSONArray getResult() {
JSONArray results = new JSONArray();
JSONArray arrays = webSession.results;
@ -193,12 +174,16 @@ public class RMemcachedServer {
}
}
startIndex = endIndex < startIndex+1000 ? endIndex : startIndex+1000;
System.out.println(results.length());
return results;
}
public static void getStats() {
webSession.getInstance().stats();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
while (webSession.stats.size() != 4){
try {
Thread.sleep(10);
@ -257,7 +242,7 @@ public class RMemcachedServer {
public bench(int runs,int nums, int threadNum, String object, String[] keys, double rate) {
this.runs = runs;
this.threadNum = threadNum;
this.object = object;
this.object = "(XXX, xxx, 2000-01-01)";
this.keys = keys;
this.size = object.length();
this.nums = nums;
@ -276,7 +261,7 @@ public class RMemcachedServer {
RMemcachedServer.threadCount --;
time = System.nanoTime() - time;
RMemcachedServer.time += time;
//System.out.println(time / 1000000000.0f);
System.err.println(time / 1000000000.0f);
}
public void randReadWrite(double scale) {
@ -284,8 +269,18 @@ public class RMemcachedServer {
int getCount = (int) (runs*scale);
int setCount = runs - getCount;
for (int i = 0, j = 0, k = 0; i < runs; i++) {
if ((Math.random() < scale || j >= setCount) && k <= getCount) {
webSession.getInstance().get(keys[randNum.nextInt(nums)]);
if ((Math.random() < scale || j >= setCount) && k < getCount) {
String keyword = keys[randNum.nextInt(nums)];
webSession.getInstance().get(keyword);
if (!webSession.middleOut.containsKey(keyword) || webSession.middleOut.get(keyword) == null
|| webSession.middleOut.get(keyword).isEmpty()) {
requestCount ++;
String value = DatabaseCon.getInstance().queryKey(keyword);
if (value == null) {
value = object;
}
webSession.getInstance().set(keyword, value);
}
k ++;
} else {
webSession.getInstance().set(keys[randNum.nextInt(nums)], object);

View File

@ -0,0 +1,294 @@
package dao;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.myself.database.DatabaseCon;
import com.myself.memcached.MemcachedMgr;
import com.myself.server.ClientConfig;
import com.myself.server.Server;
import com.myself.server.webSession;
import common.RegisterHandler;
public class WebServerMain {
static HashMap<Integer, ClientConfig> m_mapMemcachedClient;
static String ORIGINPATH = Thread.currentThread().getContextClassLoader().getResource("").getPath();
static int threadCount = 0;
private static Map<Integer, String> localStats = null;
private static JSONArray nodeStats = new JSONArray();
public static boolean initConfig() {
m_mapMemcachedClient = new HashMap<Integer, ClientConfig>();
File f = new File(System.getProperty("user.dir"));
String path = f.getPath() + File.separator + "src" + File.separator;
readClientsXML(path + "client.xml");
return true;
}
private static class bench extends Thread {
private int runs;
@SuppressWarnings("unused")
private int threadNum;
private String object;
private String[] keys;
@SuppressWarnings("unused")
private int size;
private int nums;
private double rate;
public bench(int runs, int nums, int threadNum, String object,
String[] keys, double rate) {
this.runs = runs;
this.threadNum = threadNum;
this.object = "(XXX, xxx, 2000-01-01)";
this.keys = keys;
this.size = object.length();
this.nums = nums;
this.rate = rate;
}
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// time deletes
long time = 0;
time = System.nanoTime();
randReadWrite(rate);
time = System.nanoTime() - time;
System.err.println("cost: " + time / 1000000000.0f);
WebServerMain.threadCount --;
}
public void randReadWrite(double scale) {
Random randNum = new Random();
for (int i = 0; i < runs; i++) {
if (Math.random() < scale) {
String keyword = keys[randNum.nextInt(nums)];
webSession.getInstance().get(keyword);
if (!webSession.middleOut.containsKey(keyword) || webSession.middleOut.get(keyword).isEmpty()) {
String value = DatabaseCon.getInstance().queryKey(keyword);
//System.out.println("GetfromDb: " + "key=" + keyword + ", value=" + value);
if (value == null) {
value = object;
}
webSession.getInstance().set(keyword, object);
}
} else {
webSession.getInstance().set(keys[randNum.nextInt(nums)], object);
}
try {
Thread.sleep((long) 0.00001);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
for (int i =0; i< 1; i++){
WebServerMain.run(new String[]{"10","2000","1000","8","0.8"});
}
System.exit(0);
}
public static void run(String[] args) {
PropertyConfigurator.configure(ORIGINPATH + "log4j.properties");// 加载.properties文件
initConfig();
RegisterHandler.initHandler();
webSession.getInstance().start();
// client管理
MemcachedMgr clientMgr = MemcachedMgr.getInstance();
clientMgr.init(m_mapMemcachedClient);
Server requestServer = Server.getInstance();
requestServer.init(8888);
int threads = Integer.parseInt(args[0]);// 线程数
WebServerMain.threadCount = threads;
int runs = Integer.parseInt(args[1]); // 执行次数
int Nums = Integer.parseInt(args[2]); // key数目
int size = Integer.parseInt(args[3]); // value大小
double rate = Double.parseDouble(args[4]); // 读写比例
// get object to store
byte[] obj = new byte[size];
for (int i = 0; i < size; i++) {
obj[i] = '1';
}
String value = new String(obj);
String[] keys = new String[Nums];
for (int i = 0; i < Nums; i++) {
keys[i] = "" + i;
}
getStats();
DatabaseCon.getInstance().start();
for (int i = 0; i < threads; i++) {
bench b = new bench(runs, Nums, i, value, keys, rate);
b.start();
}
while (WebServerMain.threadCount != 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println(webSession.results.get(0));
} catch (JSONException e1) {
e1.printStackTrace();
}
webSession.getInstance().stats();
while (webSession.stats.size() != 4 || webSession.results.length() < 10000){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
getStats();
System.out.println(nodeStats);
System.out.println(webSession.results.length());
System.err.println("middleOut = " + webSession.middleOut.size());
// try {
// JSONObject jStats = new JSONObject(webSession.stats.get(2));
// System.out.println(jStats);
// System.out.println(jStats.get("cmd_get"));
// System.out.println(jStats.get("cmd_set"));
// } catch (JSONException e) {
// e.printStackTrace();
// }
requestServer.stop();
//webSession.session = null;
webSession.results = new JSONArray();
}
public static void getStats() {
webSession.getInstance().stats();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.err.println(webSession.stats);
while (webSession.stats.size() != 4){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (localStats == null || localStats.size() == 0) {
localStats = webSession.stats;
} else {
try {
for (int i = 0; i < 4; i++){
int key = (int) Math.pow(2, i);
JSONObject jStats_new = new JSONObject(webSession.stats.get(key));
JSONObject jStats_old = new JSONObject(localStats.get(key));
int cmd_get_old = Integer.parseInt((String) jStats_old.get("cmd_get"));
int cmd_set_old = Integer.parseInt((String) jStats_old.get("cmd_set"));
int cmd_get_new = Integer.parseInt((String) jStats_new.get("cmd_get"));
int cmd_set_new = Integer.parseInt((String) jStats_new.get("cmd_set"));
int cmd_get = cmd_get_new - cmd_get_old;
int cmd_set = cmd_set_new - cmd_set_old;
Map<String, Integer> node = new HashMap<>();
node.put("node", i);
node.put("get", cmd_get);
node.put("set", cmd_set);
nodeStats.put(node);
}
} catch (Exception e) {
e.printStackTrace();
}
}
webSession.stats = new HashMap<>();
}
// 读取memcached client配置
public static boolean readClientsXML(String str) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document xmldoc = db.parse(new File(str));
Element elmtInfo = xmldoc.getDocumentElement();
NodeList nodes = elmtInfo.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node result = nodes.item(i);
if (result.getNodeType() == Node.ELEMENT_NODE
&& result.getNodeName().equals("client")) {
NodeList ns = result.getChildNodes();
ClientConfig localClient = new ClientConfig();
int m = 0;
for (int j = 0; j < ns.getLength(); j++) {
Node record = ns.item(j);
if (record.getNodeType() == Node.ELEMENT_NODE) {
if (record.getNodeName().equals("id")) {
m++;
localClient.id = Integer.decode(record
.getTextContent());
} else if (record.getNodeName().equals("host")) {
m++;
localClient.host = record.getTextContent();
} else if (record.getNodeName().equals(
"client_port")) {
m++;
localClient.client_port = Integer.decode(record
.getTextContent());
} else if (record.getNodeName().equals("memcached")) {
m++;
localClient.memcached = record.getTextContent();
}
}
}
if (m == 4) {
m_mapMemcachedClient.put(localClient.id, localClient);
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}

View File

@ -1,3 +1,6 @@
/**
* @author martji magq@act.buaa.edu.cn
*/
package servlets;
import java.io.IOException;