Rails 奇怪的错误 MemCache::MemCacheError (Timeout::Error: IO timeout)

iceskysl · 2012年06月06日 · 最后由 iceskysl 回复于 2012年06月08日 · 7033 次阅读

之前好像好好的项目,最近频繁收到报错,信息如下

MemCache::MemCacheError (No connection to server (192.168.0.2:11211 DEAD (Timeout::Error: IO timeout), will retry at Wed Jun 06 18:16:17 +0800 2012)):

启动 memcached 是这样得

memcached -d -m 1024m -uroot -l192.168.0.5 -c 10240
[root@ice ~]# memcached -h
memcached 1.4.5

用得是 gems 如下

Using memcache-client (1.8.5)
Using unicorn (4.1.1)

代码如下

def show_default_keyword
  @keyword = SearchWord.caches(:get_default_keyword,:ttl => 30.minutes)
end  

谁有遇到类似的错误,如何排查?

为什么地址是 192.168.0.2?启动的不是 05 吗? 如果是少量的的可以通过捕获 IO 错误之后返回 nil 来处理:

class MemCache
  def cache_get_with_timeout_protection(*args)
    begin
      cache_get_without_timeout_protection(*args)
    rescue MemCache::MemCacheError => e
      if e.to_s == 'IO timeout' and (Rails.env.production? or Rails.env.staging?)
        nil
      else
        raise e
      end
    end
  end
  alias_method_chain :cache_get, :timeout_protection
end

#1 楼 @hooopo 写错了,都是 0.5~

通过捕获 IO 错误之后返回 nil 来处理只是不抛出,但是还是没找到原因是什么,理论上压力不大,memcached 不应该出现类似的错误呢~

memcached 和 app server 直接有无防火墙?以前遇到过防火墙策略调整,导致杀死不活跃的长连接,错误和你这个类似。

匿名 #4 2012年06月06日

telnet 192.168.0.5 11211 Does it work well ?

如果没有说明没有起来,可以尝试 memcached -d -m 1024m -uroot -l 127.0.0.1 -c 1024

当然也可以把-d 去掉,看看到底是什么原因没有起来,如果正常起来,八成是 sasl 验证的问题了

BTW: 推荐 dalli,memcache-client 不在维护

#4 楼 @sharp telnet 192.168.0.5 11211是没问题的~

dalli 看到了,下次可以换掉~

#3 楼 @quakewang 好像没开防火墙~ 最开始查的就是防火墙~

匿名 #7 2012年06月06日

不知道你的 memcache-client 有没有升级过?我忘了是不是 memcache-client 的新版本处于安全性的考虑默认开启 sasl,所以你的 memcache 也要 enable sasl, 如果采用 ubuntu 倒是好办

apt-get install libsasl2-2 libsasl2-dev
./configure --enable-sasl 
make
make install

查查系统资源,比如 CPU, Memory, 看看是不是在某个瞬间爆了。

@iceskysl 1.尝试下将 connect timeout 的时间设置的长一些 memcached .... -c 10240 怎么会设置这么大的一个数值? 2.可能是 memcache response 的时间太长了,导致 socket timeout 了

话说,ruby-china 的这个 at 的功能用起来真是舒服,赞一个!!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号