配置了2个节点的Spymemcached问题

时间:2017-07-10 10:15:03

标签: memcached spymemcached

我正在为我的项目使用Ketama算法的spymemcached。我有两个memcached服务器作为HA(高可用性)的一部分运行,我的配置是

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=kr.pe.kwonnam.hibernate4memcached.Hibernate4MemcachedRegionFactory
hibernate.cache.default_cache_concurrency_strategy=NONSTRICT_READ_WRITE
hibernate.cache.region_prefix=myProjectCache
hibernate.cache.use_structured_entries=false
h4m.adapter.class=kr.pe.kwonnam.hibernate4memcached.spymemcached.SpyMemcachedAdapter
h4m.timestamper.class=kr.pe.kwonnam.hibernate4memcached.timestamper.HibernateCacheTimestamperMemcachedImpl

h4m.adapter.spymemcached.hosts=host1:11211,host2:11211
h4m.adapter.spymemcached.hashalgorithm=KETAMA_HASH
h4m.adapter.spymemcached.operation.timeout.millis=5000
h4m.adapter.spymemcached.transcoder=kr.pe.kwonnam.hibernate4memcached.spymemcached.KryoTranscoder
h4m.adapter.spymemcached.cachekey.prefix=myProject
h4m.adapter.spymemcached.kryotranscoder.compression.threashold.bytes=20000

# 10 minutes
h4m.expiry.seconds=600
# a day
h4m.expiry.seconds.validatorCache.org.hibernate.cache.spi.UpdateTimestampsCache=86400
# 1 hour
h4m.expiry.seconds.validatorCache.org.hibernate.cache.internal.StandardQueryCache=3600
# 30 minutes
h4m.expiry.seconds.myProjectCache.database1=1800
h4m.expiry.seconds.myProjectCache.database2=1800

按照以下链接进行配置:

SpyMemcachedAdapter

两个节点host1和host2都可以访问,启动并运行。

问题:

作为测试HA的一部分,当我关闭一个memcached(host1)时,我的应用程序正在连接到host2,但只是在尝试连接host1(对于每个请求都将是timedout - 因为host1已关闭)之后。这会导致花费太多时间 以下是每个请求抛出的异常

2017-07-07 17:27:31.915 [SimpleAsyncTaskExecutor-6] ERROR u.c.o.sProcessor - TransId:004579 - Exception occurred while processing request :Timeout waiting for value: waited 5,000 ms. Node status: Connection Status { /host1:11211 active: false, authed: true, last read: 247,290 ms ago /host2:11211 active: true, authed: true, last read: 5 ms ago }
 2017-07-07 17:28:54.666 INFO net.spy.memcached.MemcachedConnection:  Reconnecting due to failure to connect to {QA sa=/host1:11211, #Rops=0, #Wops=214, #iq=0, topRop=null, topWop=Cmd: 5 Opaque: 341143 Key: myProject.myProjectCache.databse1@ Amount: 0 Default: 1499444604639 Exp: 2592000, toWrite=0, interested=0}
    java.net.ConnectException: Connection refused
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
        at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:677)
        at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:436)
        at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:1446)
    2017-07-07 17:28:54.666 WARN net.spy.memcached.MemcachedConnection:  Closing, and reopening {QA sa=/host1:11211, #Rops=0, #Wops=214, #iq=0, topRop=null, topWop=Cmd: 5 Opaque: 341143 Key: myProject.myProjectCache.databse1@ Amount: 0 Default: 1499444604639 Exp: 2592000, toWrite=0, interested=0}, attempt 14.
    2017-07-07 17:28:54.841 WARN net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for myProject.myProjectCache.databse1@-1:my.co.org.myProject.dao.entity.databse1.tablexyz#14744.

我是第一次使用memcached,不确定这是否是spymemcached的行为?或者我在配置中遗漏了什么?要么 通过更改超时配置,它将解决处理请求所需的时间吗?

任何建议/帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

如果您使用的是使用OOTB ConnectionFactoryBuilder的DefaultConnectionFactory,则在失败的操作计数达到timeoutExceptionThreshold(在spymemcached的2.7版中)初始化为998后,将发生重新连接。因此,如果您创建自己的ConnectionFactory并将timeoutExceptionThreshold更改为较低的值那么您应该会看到自动恢复。

希望这会有所帮助。