0

我在集群环境中使用 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 usage="read-write"><invalidation-cache>应该是一个工作场景的组合。在我的测试中,我有一个节点持续读取一个对象,一个节点更新同一个对象。我发现读取节点永远不会收到通知对象已更改以便从数据库中再次读取它。通过查看日志,似乎写入节点上的失效缓存没有向读取节点发送任何消息。

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

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

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

谢谢!

4

1 回答 1

0

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

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

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

于 2020-11-06T09:08:21.923 回答