Extract variables which can be configured and fix some bug
This commit is contained in:
parent
d7dc51651c
commit
3680c3debc
|
@ -3,6 +3,7 @@ package client;
|
|||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import server.ClientConfig;
|
||||
|
||||
public class ClientMgr
|
||||
|
@ -10,7 +11,11 @@ public class ClientMgr
|
|||
public HashMap<Integer, Client> m_mapLocalClients;
|
||||
static ClientMgr clientMgr;
|
||||
public int mClientNumber;
|
||||
public static final int nCopyNode = 4;
|
||||
public static int nCopyNode;
|
||||
public static int protocol;
|
||||
public static final Integer twoPhaseCommit = 0;
|
||||
public static final Integer paxos = (nCopyNode-1)-((nCopyNode-1)/2+1);
|
||||
public static final Integer weak = nCopyNode-2;
|
||||
|
||||
public static ClientMgr getInstance()
|
||||
{
|
||||
|
@ -25,6 +30,7 @@ public class ClientMgr
|
|||
return m_mapLocalClients.size()+1;
|
||||
}
|
||||
|
||||
// num 为hash第一个节点
|
||||
public boolean isCopyNode(Integer hash)
|
||||
{
|
||||
if ((mClientNumber-hash + m_mapLocalClients.size()+1)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.myself.server;
|
||||
package server;
|
||||
|
||||
public class LockKey {
|
||||
public Integer memNumber = 0;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.myself.server;
|
||||
package server;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.channel.Channel;
|
||||
|
@ -7,7 +7,7 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder;
|
|||
|
||||
import common.EMSGID;
|
||||
|
||||
public class MDecoder extends FrameDecoder //Decoder stream
|
||||
public class MDecoder extends FrameDecoder
|
||||
{
|
||||
@Override
|
||||
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.myself.server;
|
||||
package server;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.buffer.ChannelBuffers;
|
||||
|
@ -6,7 +6,7 @@ import org.jboss.netty.channel.Channel;
|
|||
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
|
||||
|
||||
public class MEncoder extends OneToOneEncoder //Encoder Message
|
||||
public class MEncoder extends OneToOneEncoder
|
||||
{
|
||||
@Override
|
||||
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.myself.server;
|
||||
package server;
|
||||
|
||||
import org.jboss.netty.channel.Channel;
|
||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
|
@ -11,13 +11,13 @@ import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
|||
public class MServerHandler extends SimpleChannelUpstreamHandler
|
||||
{
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) //handling received message
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
{
|
||||
if (!(e.getMessage() instanceof NetMsg))
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Put this Message into queue, and then handle it.
|
||||
memSession.getInstance().addSession(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -34,6 +34,29 @@ public class MemcachedMain {
|
|||
File f = new File(System.getProperty("user.dir"));
|
||||
String path = f.getPath() + File.separator + "bin" + File.separator;
|
||||
readClientsXML(path + "client.xml");
|
||||
try {
|
||||
Properties properties = new 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();
|
||||
if(protocolName.equals("twoPhaseCommit")){
|
||||
ClientMgr.protocol = ClientMgr.twoPhaseCommit;
|
||||
}else if(protocolName.equals("paxos")){
|
||||
ClientMgr.protocol = ClientMgr.paxos;
|
||||
}else if(protocolName.equals("weak")){
|
||||
ClientMgr.protocol = ClientMgr.weak;
|
||||
}else{
|
||||
System.err.print("consistency protocol input error");
|
||||
return false;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -57,7 +80,7 @@ public class MemcachedMain {
|
|||
clientMgr.init(num, m_mapMemcachedClient);
|
||||
|
||||
Client webClient = new Client();
|
||||
webClient.init("192.168.3.222", 8888);
|
||||
webClient.init(webServerHost, 8888);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.myself.server;
|
||||
package server;
|
||||
import com.google.protobuf.GeneratedMessage;
|
||||
import com.google.protobuf.MessageLite;
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ public class memSession implements Runnable {
|
|||
System.out.println("session start");
|
||||
}
|
||||
|
||||
|
||||
// Ôö¼ÓclientÁ¬½Ó
|
||||
public void addClientChannel(Integer num, Channel ch) {
|
||||
ClientChannelMap.put(num, ch);
|
||||
}
|
||||
|
@ -58,6 +58,7 @@ public class memSession implements Runnable {
|
|||
return ClientChannelMap.get(id);
|
||||
}
|
||||
|
||||
// ɾµôclientÁ¬½Ó
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void removeClientChannel(Channel ch) {
|
||||
Iterator iter = ClientChannelMap.entrySet().iterator();
|
||||
|
@ -403,7 +404,7 @@ public class memSession implements Runnable {
|
|||
case nm_write_1_res: {
|
||||
nm_write_1_res msgLite = msg.getMessageLite();
|
||||
|
||||
if (desLockKeyCount(msgLite.getKey()) == 0) {
|
||||
if (desLockKeyCount(msgLite.getKey()) == ClientMgr.protocol) {
|
||||
boolean res = client.set(msgLite.getKey(),
|
||||
msgLite.getValue());
|
||||
if (res) {
|
||||
|
@ -466,6 +467,10 @@ public class memSession implements Runnable {
|
|||
recvQueue.offer(e);
|
||||
}
|
||||
|
||||
public int gethashMem(String key) {
|
||||
return Math.abs(key.hashCode() % ClientMgr.getInstance().getSize());
|
||||
}
|
||||
|
||||
public boolean sendOtherCopyMsg(Integer hash, NetMsg msg) {
|
||||
for (int i = 0; i < ClientMgr.nCopyNode; i++) {
|
||||
Integer index = (hash + i + ClientMgr.getInstance().getSize())
|
||||
|
|
Loading…
Reference in New Issue