memcached 客户端代码 Java memcached client学习(一致性hash)

昨天写了一篇短文 描述了淘宝面试的一些题。 
今天下午 装了在ubuntu中装了 memcached ,装起来还是很简单的。 
主要是装一下。 

Java代码   收藏代码
  1. memcached: wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz  /data/  
  2. libevent:wget  http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz   /  


自己分配了一个端口。那么我想还是从最简单的memcached  -- 
java memcached client源码,总的代码量还是很少的 
主要是如下两个类: 
MemcachedClient.java 
SockIOPool.java 

好 先看推荐的测试代码: 
Java代码   收藏代码
  1. /** 
  2.  * Copyright (c) 2008 Greg Whalin 
  3.  * All rights reserved. 
  4.  * 
  5.  * This library is free software; you can redistribute it and/or 
  6.  * modify it under the terms of the BSD license 
  7.  * 
  8.  * This library is distributed in the hope that it will be 
  9.  * useful, but WITHOUT ANY WARRANTY; without even the implied 
  10.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  11.  * PURPOSE. 
  12.  * 
  13.  * You should have received a copy of the BSD License along with this 
  14.  * library. 
  15.  * 
  16.  * @author greg whalin <[email protected] 
  17.  */  
  18. package com.meetup.memcached.test;  
  19.   
  20. import com.meetup.memcached.*;  
  21. import org.apache.log4j.*;  
  22.   
  23. public class TestMemcached  {    
  24.     public static void main(String[] args) {  
  25.               // memcached should be running on port 11211 but NOT on 11212  
  26.   
  27.         BasicConfigurator.configure();  
  28.         String[] servers = { "localhost:11211"};  
  29.         SockIOPool pool = SockIOPool.getInstance();  
  30.         pool.setServers( servers );  
  31.         pool.setFailover( true );  
  32.         pool.setInitConn( 10 );   
  33.         pool.setMinConn( 5 );  
  34.         pool.setMaxConn( 250 );  
  35.         pool.setMaintSleep( 30 );  
  36.           //这是开启一个nagle 算法。改算法避免网络中充塞小封包,提高网络的利用率  
  37.         pool.setNagle( false );  
  38.         pool.setSocketTO( 3000 );  
  39.         pool.setAliveCheck( true );  
  40.         pool.initialize();  
  41.   
  42.         MemcachedClient mcc = new MemcachedClient();  
  43.   
  44.         // turn off most memcached client logging:  
  45.         com.meetup.memcached.Logger.getLogger( MemcachedClient.class.getName() ).setLevel( com.meetup.memcached.Logger.LEVEL_WARN );  
  46.   
  47.         for ( int i = 0; i < 10; i++ ) {  
  48.             boolean success = mcc.set( "" + i, "Hello!" );  
  49.             String result = (String)mcc.get( "" + i );  
  50.             System.out.println( String.format( "set( %d ): %s", i, success ) );  
  51.             System.out.println( String.format( "get( %d ): %s", i, result ) );  
  52.         }  
  53.   
  54. }  


其实 对于我来说 我很想明白的是连接池是如何配置的,关键在于 pool.initialize(); 这个方法如何初始化的。 

Java代码   收藏代码
  1. /**  
  2.      * Initializes the pool.  
  3.      */  
  4.     public void initialize() {  
  5.   
  6.         synchronizedthis ) {  
  7.   
  8.             // check to see if already initialized  
  9.             if ( initialized  
  10.                     && ( buckets != null || consistentBuckets != null )  
  11.                     && ( availPool != null )  
  12.                     && ( busyPool != null ) ) {  
  13.                 log.error( "++++ trying to initialize an already initialized pool" );  
  14.                 return;  
  15.             }  
  16.   
  17.             // pools  
  18.             availPool   = new HashMap<String,Map<SockIO,Long>>( servers.length * initConn );  
  19.             busyPool    = new HashMap<String,Map<SockIO,Long>>( servers.length * initConn );  
  20.             deadPool    = new IdentityHashMap<SockIO,Integer>();  
  21.   
  22.             hostDeadDur = new HashMap<String,Long>();  
  23.             hostDead    = new HashMap<String,Date>();  
  24.             maxCreate   = (poolMultiplier > minConn) ? minConn : minConn / poolMultiplier;       // only create up to maxCreate connections at once  
  25.   
  26.             if ( log.isDebugEnabled() ) {  
  27.                 log.debug( "++++ initializing pool with following settings:" );  
  28.                 log.debug( "++++ initial size: " + initConn );  
  29.                 log.debug( "++++ min spare   : " + minConn );  
  30.                 log.debug( "++++ max spare   : " + maxConn );  
  31.             }  
  32.   
  33.             // if servers is not set, or it empty, then  
  34.             // throw a runtime exception  
  35.             if ( servers == null || servers.length <= 0 ) {  
  36.                 log.error( "++++ trying to initialize with no servers" );  
  37.                 throw new IllegalStateException( "++++ trying to initialize with no servers" );  
  38.             }  
  39.   
  40.             // initalize our internal hashing structures  
  41.             if ( this.hashingAlg == CONSISTENT_HASH )  
  42.                 populateConsistentBuckets();  
  43.             else  
  44.                 populateBuckets();  
  45.   
  46.             // mark pool as initialized  
  47.             this.initialized = true;  
  48.   
  49.             // start maint thread  
  50.             if ( this.maintSleep > 0 )  
  51.                 this.startMaintThread();  
  52.         }  
  53.     }  


如上代码流程如下: 
1  检测是否已经被初始化 
2  定义可用链接 ,繁忙链接池 
3  判断是否一致性hash算法 还是普通的算法 
4  定义一个后台线程 ,来维护 
好 ,首先来分析下一致性hash算法。 

从如下代码来分析 : 
Java代码   收藏代码
  1.     if (   
  2.               this.hashingAlg == CONSISTENT_HASH )  
  3. populateConsistentBuckets();  


Java代码   收藏代码
  1. /** 将server添加到一致性hash的2的32次 圆环  **/  
  2.     private void populateConsistentBuckets() {  
  3.         if ( log.isDebugEnabled() )  
  4.             log.debug( "++++ initializing internal hashing structure for consistent hashing" );  
  5.   
  6.         // store buckets in tree map   
  7.         this.consistentBuckets = new TreeMap<Long,String>();  
  8.   
  9.         MessageDigest md5 = MD5.get();  
  10.         //得到总的权重  
  11.         if ( this.totalWeight <= 0 && this.weights !=  null ) {  
  12.             for ( int i = 0; i < this.weights.length; i++ )  
  13.                 this.totalWeight += ( this.weights[i] == null ) ? 1 : this.weights[i];  
  14.         }  
  15.         else if ( this.weights == null ) {  
  16.             this.totalWeight = this.servers.length;  
  17.         }  
  18.           
  19.       
  20.         for ( int i = 0; i < servers.length; i++ ) {  
  21.             //每台服务器的权重  
  22.             int thisWeight = 1;  
  23.             if ( this.weights != null && this.weights[i] != null ) {  
  24.                 thisWeight = this.weights[i];  
  25.             }  
  26.               
  27.             //有兴趣的朋友可以参考平衡Hash 算法的另一个指标是平衡性 (Balance) ,定义如下:    平衡性 平衡性是指哈希的结果能够尽可能分布到所有的缓冲中去,这样可以使得所有的缓冲空间都得到利用  
  28.             //了解决这种情况, consistent hashing 引入了“虚拟节点”的概念,它可以如下定义: “虚拟节点”( virtual node )是实际节点在 hash 空间的复制品( replica ),一实际个节点对应了若干个“虚拟节点”,这个对应个数也成为“复制个数”,“虚拟节点”在 hash 空间中以 hash 值排列。  
  29.             double factor = Math.floor(((double)(40 * this.servers.length * thisWeight)) / (double)this.totalWeight);  
  30.               
  31.             for ( long j = 0; j < factor; j++ ) { //加密规则类似 127.0.0.1_1    
  32.                 byte[] d = md5.digest( ( servers[i] + "-" + j ).getBytes() ); //转化成16位的字节数组  
  33.                 //16位二进制数组每4位为一组,每组第4个值左移24位,第三个值左移16位,第二个值左移8位,第一个值不移位。进行或运算,得到一个小于2的32 次方的long值  
  34.                 for ( int h = 0 ; h < 4; h++ ) { //因为是16位   
  35.                     Long k =  //实际上每个字节进行了运算  
  36.                           ((long)(d[3+h*4]&0xFF) << 24)   
  37.                         | ((long)(d[2+h*4]&0xFF) << 16)   
  38.                         | ((long)(d[1+h*4]&0xFF) << 8)    
  39.                         | ((long)(d[0+h*4]&0xFF));  
  40.   
  41.                     consistentBuckets.put( k, servers[i] );  
  42.                     if ( log.isDebugEnabled() )  
  43.                         log.debug( "++++ added " + servers[i] + " to server bucket" );  
  44.                 }                 
  45.             }  
  46.               
  47.               
  48.   
  49.             // create initial connections  
  50.             if ( log.isDebugEnabled() )  
  51.                 log.debug( "+++ creating initial connections (" + initConn + ") for host: " + servers[i] );  
  52.   
  53.             //创建链接  
  54.             for ( int j = 0; j < initConn; j++ ) {  
  55.                 SockIO socket = createSocket( servers[i] );  
  56.                 if ( socket == null ) {  
  57.                     log.error( "++++ failed to create connection to: " + servers[i] + " -- only " + j + " created." );  
  58.                     break;  
  59.                 }  
  60.                 //加入socket到连接池 这里慢慢谈  
  61.                 addSocketToPool( availPool, servers[i], socket );  
  62.                 if ( log.isDebugEnabled() )  
  63.                     log.debug( "++++ created and added socket: " + socket.toString() + " for host " + servers[i] );  
  64.             }  
  65.         }  
  66.           
  67.     }  


好 比如说 我们调用了 如下代码: 
Java代码   收藏代码
  1. MemcachedClient mcc = new MemcachedClient();  
  2. mcc.set("6"1);  


这里key 如何定位到一台server呢?我先把一致性hash算法的定位方法说下。 
Java代码   收藏代码
  1. //得到定位server的Socket封装对象  
  2. SockIOPool.SockIO sock = pool.getSock( key, hashCode );  


Java代码   收藏代码
  1. //计算出key对应的hash值(md5) ,然后  
  2. long bucket = getBucket( key, hashCode );  


Java代码   收藏代码
  1. //得到大于hash的map,因为treemap已经排好序了。调用tailMap可以得到大于等于这个hash的对象 ,然后调用firstKey得到圆环上的hash值  
  2. SortedMap<Long,String> tmap =  
  3.             this.consistentBuckets.tailMap( hv );  
  4.         return ( tmap.isEmpty() ) ? this.consistentBuckets.firstKey() : tmap.firstKey();  



明天开始继续看代码 ,还是要坚持。 
一致性hash的算法参考 
http://xok.la/2010/06/memcache_consistent_hashing.html 

猜你喜欢

转载自mowengaobo.iteye.com/blog/1500061