【问题标题】:Hibernate with infinispan invalidation cache in a cluster在集群中使用 infinispan 失效缓存休眠
【发布时间】:2021-02-18 02:28:39
【问题描述】:

我在集群环境中使用 Hibernate 5.4.22 和 Infinispan 11.0.4。 Hibernate 二级缓存配置为使用 JCache 提供程序:

    hbProps.setProperty("hibernate.cache.use_minimal_puts", "false");
    hbProps.setProperty("hibernate.cache.use_structured_entries", "false");
    hbProps.setProperty("hibernate.cache.use_query_cache", "false");
    hbProps.setProperty("hibernate.cache.use_second_level_cache", "true");
    hbProps.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.jcache.JCacheRegionFactory");
    hbProps.setProperty("hibernate.javax.cache.provider", "org.infinispan.jcache.embedded.JCachingProvider");

Infinispan 配置有以下 infinispan.xml:

<infinispan
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:infinispan:config:11.0 http://www.infinispan.org/schemas/infinispan-config-11.0.xsd"
      xmlns="urn:infinispan:config:11.0">
      
      <jgroups>
          <stack name="jgroups-stack">
              <TCP bind_port="7800"
                   recv_buf_size="${tcp.recv_buf_size:130k}"
                   send_buf_size="${tcp.send_buf_size:130k}"
                   max_bundle_size="64K"
                   sock_conn_timeout="300"
          
                   thread_pool.min_threads="0"
                   thread_pool.max_threads="20"
                   thread_pool.keep_alive_time="30000"/>
          
<!--              <TCPPING async_discovery="true"
                       initial_hosts="${jgroups.tcpping.initial_hosts:localhost[7800],localhost[7801]}"
                       port_range="2"/>-->
              <MPING/>
              <MERGE3  min_interval="10000"
                       max_interval="30000"/>
              <FD_SOCK/>
              <FD_ALL timeout="9000" interval="3000" />
              <VERIFY_SUSPECT timeout="1500"  />
              <BARRIER />
              <pbcast.NAKACK2 use_mcast_xmit="false"
                             discard_delivered_msgs="true"/>
              <UNICAST3 />
              <pbcast.STABLE desired_avg_gossip="50000"
                             max_bytes="4M"/>
              <pbcast.GMS print_local_addr="false" join_timeout="2000"/>
              <UFC max_credits="2M"
                   min_threshold="0.4"/>
              <MFC max_credits="2M"
                   min_threshold="0.4"/>
              <FRAG2 frag_size="60K"  />
              <!--RSVP resend_interval="2000" timeout="10000"/-->
              <pbcast.STATE_TRANSFER/>                    
          </stack>
      </jgroups> 
      
      <cache-container>
          <!-- turn off metrics -->
          <metrics gauges="false" histograms="false"/>
          <jmx enabled="true"/>
          <transport stack="jgroups-stack" cluster="infinispan-hibernate-cluster"/>
          
          <serialization marshaller="org.infinispan.commons.marshall.JavaSerializationMarshaller">
            <white-list>
              <regex>.*</regex>
            </white-list>   
          </serialization>
          
<!--          <replicated-cache name="MainCache" mode="SYNC"> 
            <memory max-count="100000"/>
            <expiration max-idle="3600000" lifespan="-1"/>
          </replicated-cache>-->

          <invalidation-cache name="MainCache" mode="SYNC">
            <memory max-count="100000"/>
            <expiration max-idle="3600000" lifespan="-1"/>
          </invalidation-cache>
      </cache-container>
</infinispan>

我有一个测试实体“配置文件”,它应该使用read-write 策略缓存:

<hibernate-mapping>

  <class name="hibernatetest.Profile" table="Profiles" lazy="false">
    <cache usage="read-write" region="MainCache"/>
    <id column="id" name="id" unsaved-value="0">
      <generator class="native"/>
    </id>
    <discriminator column="type" type="string"/>
    <version name="version"/>
    <property name="name" type="string" length="100"/>
    <property name="available" type="yes_no"/>
  </class>
  
</hibernate-mapping>

根据Cache concurrency strategy/cache mode compatibility table&lt;cache usage="read-write"&gt;&lt;invalidation-cache&gt; 的组合应该是一个工作场景。在我的测试中,我有一个节点持续读取一个对象,一个节点更新同一个对象。我发现读取节点永远不会收到对象已更改的通知,以便再次从数据库中读取它。通过查看日志,似乎写节点上的失效缓存没有向读节点发送任何消息。

但是,如果我将 infinispan.xml 更改为使用 &lt;replicated-cache&gt;&lt;distributed-cache&gt; 而不是 &lt;invalidation-cache&gt;,则读取节点会收到写入节点所做更改的通知。所以,我想这意味着问题不在 jgroups 中。

这是失效缓存中的问题,还是我的配置中缺少某些内容?

这是从失效缓存inv-read.txt读取的实例的日志文件和更新缓存inv-update.txt的实例的日志文件。

谢谢!

【问题讨论】:

    标签: hibernate infinispan cache-invalidation


    【解决方案1】:

    问题在于使用 JCache - 该表假定 InfinispanRegionFactory 而不是 JCacheRegionFactory。

    似乎 Infinispan 没有明确支持 Hibernate 5.4 的模块 - 我猜想 modules to support Hibernate 5.3 即使在 Hibernate 5.4 上也应该工作,因为二级缓存领域没有太大变化在休眠 5.4 中。

    我很惊讶 2LC 完全可以与 JCache 一起使用复制/分布式缓存 - 我很确定它无论如何都不能“可靠地”工作(事务上,覆盖边缘情况等)。

    【讨论】:

    • 我尝试了 InfinispanRegionFactory (infinispan-hibernate-cache-v53) 版本 11.0.4,它适用于 Hibernate 5.3 和 5.4。非常感谢!
    猜你喜欢
    • 2015-11-07
    • 2013-05-29
    • 1970-01-01
    • 2016-06-08
    • 2023-03-03
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多