<< linux 系统监控、诊断工具之 IO wait - leejun_2005的个人页面 - 开源中国社区 | 首页 | 分享转发:做股票应懂的十二条投资数学_巴曙松 >>

云计算之路-柳暗花明:为什么memcached会堵车 - 博客园团队 - 博客园

一个故障期间的重要现象闪现在眼前——当时memcached的磁盘IO高!

memcached缓存的数据都在内存中,而且内存占用并不高,磁盘IO怎么会高?太奇怪了!

。。。

通过google搜索“memcached read timeout”,找到柳暗花明的线索——memcached timeout error because of slow response

直接看关键文字:

The problem seemed to boil down to the following:
vm.swappiness=60 (default) is a very bad idea, when combined
with deadline as a io scheduler.

...

conclusion:
if you use memcache and need high amounts of memory with
many objects, keep a look at your swap, and if there is
something in it (even 1 kb) - it might be too much.
after setting vm.swappiness to zero and paging in all swap,
the effects were gone.

 登上昨天引发故障的那台memcached服务器,运行命令:

cat /proc/sys/vm/swappiness 60

输出结果是60!磁盘IO高就是内存交换引起的!memcached堵车的原因就在这!

只要将swappiness设置为0,就能解决问题,设置方法参考:Adjust Your swappiness

 

关于memcached的连接超时:

对于用户来说,最主要的功能是存取数据,假设我们有一个 memcached 节点 IP 地址或者域名是 host ,端口是 11211 ,一个简单的存取数据的例子如下:



MemcachedClientBuilder builder = new XMemcachedClientBuilder(


               AddrUtil.getAddresses (“localhost:11211”));


MemcachedClient memcachedClient = builder.build();


       try {


           memcachedClient. set ( "hello" , 0, "Hello,xmemcached" );

           String value = memcachedClient. get ( "hello" );


           System. out .println( "hello=" + value);



           memcachedClient. delete ( "hello" );


           value = memcachedClient.get( "hello" );


           System. out .println( "hello=" + value);



       } catch (MemcachedException e) {


           System. err .println( "MemcachedClient operation fail" );


           e.printStackTrace();


       } catch (TimeoutException e) {


           System. err .println( "MemcachedClient operation timeout" );


           e.printStackTrace();


       } catch (InterruptedException e) {

           // ignore

       }


       try {


           memcachedClient.shutdown();


       } catch (IOException e) {


           System. err .println( "Shutdown MemcachedClient fail" );


           e.printStackTrace();


       }



因为 XMemcachedClient 的创建有比较多的可选项,因此提供了一个 XMemcachedClientBuilder 用于构建 MemcachedClient 。 MemcachedClient 是主要接口,操作 memcached 的主要方法都在这个接口里, XMemcachedClient 是它的一个实现。传入的 memcached 节点列表要求是类似 ”host1:port1 host2:port2 …” 这样的字符串,通过 AddrUtil.getAddresses 方法获取实际的 IP 地址列表。存储数据是通过 set 方法,它有三个参数,第一个是存储的 key 名称,第二个是 expire 时间(单位秒) ,超过这个时间 ,memcached 将这个数据替换出去, 0 表示永久存储(默认是一个月) ,第三个参数就是实际存储的数据,可以是任意的 java 可序列化类型 。 获取存储的数据是通过 get 方法,传入 key 名称即可。如果要删除存储的数据,这是通过 delete 方法,它也是接受 key 名称作为参数。 XMemcached 由于是基于 nio ,因此通讯过程本身是异步的, client 发送一个请求给 memcached ,你是无法确定 memcached 什么时候返回这个应答,客户端此时只有等待,因此还有个等待超时的概念在这里。客户端在发送请求后,开始等待应答,如果超过一定时间就认为操作失败,这个等待时间默认是一秒,上面例子展现的 3 个方法调用的都是默认的超时时间,这三个方法同样有允许传入超时时间的重载方法,例如


Value=client.get(“hello”,3000);


就是等待 3 秒超时,如果 3 秒超时就跑出 TimeutException ,用户需要自己处理这个异常。因为等待是通过调用 CountDownLatch.await(timeout) 方法,因此用户还需要处理中断异常 InterruptException 。最后的 MemcachedException 表示 Xmemcached 内部发生的异常,如解码编码错误、网络断开等等异常情况。

阅读全文……

标签 : , ,



发表评论 发送引用通报