Apache Ignite: 重新连接到Ignite服务器后,缓存无法使用

5

我正在使用Apache Ignite作为分布式缓存,但遇到了一些基本的健壮性问题。如果我们的Ignite服务器由于任何原因重新启动,似乎会导致所有的Ignite客户端都无法正常工作,即使在Ignite服务器恢复在线后。

以下是客户端在与缓存交互之后服务器重新启动并且客户端重新连接时看到的错误:

Caused by: org.apache.ignite.internal.processors.cache.CacheStoppedException: Failed to perform cache operation (cache is stopped): <redacted>

我的期望是当Ignite服务器重新上线时,Ignite客户端应该能够重新连接到服务器并继续工作。从我所了解的资料来看, 厚客户端应该可以做到这一点,但我没有看到此现象发生。为什么缓存仍被认为是停止状态?

我们正在使用带有Kubernetes IP查找器的Ignite 2.7.6版本。

1个回答

6

看起来你正在使用一个过期的缓存代理。
如果你正在使用内存集群,并且从客户端动态创建了一个缓存,那么当集群重新启动时,这个缓存将消失。

下面的代码在客户端对内存集群执行时,如果所涉及的缓存不是服务器配置的一部分,而是在客户端上动态创建的,则在集群重新启动时会生成异常。

       Ignition.setClientMode(true);
       Ignite = Ignition.start();

       IgniteCache cache = ignite.getOrCreateCache("mycache"); //dynamically created cache


        int counter = 0;
        while(true) {
            try {
                cache.put(counter, counter);
                System.out.println("added counter: " + counter);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

生成
java.lang.IllegalStateException: class org.apache.ignite.internal.processors.cache.CacheStoppedException: Failed to perform cache operation (cache is stopped): mycache
    at org.apache.ignite.internal.processors.cache.GridCacheGateway.enter(GridCacheGateway.java:164)
    at org.apache.ignite.internal.processors.cache.GatewayProtectedCacheProxy.onEnter(GatewayProtectedCacheProxy.java:1555)

你需要注意断开连接的事件/异常。
参见:https://ignite.apache.org/docs/latest/clustering/connect-client-nodes
IgniteCache cache = ignite.getOrCreateCache(cachecfg);

try {
    cache.put(1, "value");
} catch (IgniteClientDisconnectedException e) {
    if (e.getCause() instanceof IgniteClientDisconnectedException) {
        IgniteClientDisconnectedException cause = (IgniteClientDisconnectedException) e.getCause();

        cause.reconnectFuture().get(); // Wait until the client is reconnected.
        // proceed

如果这是由多个基准节点组成的持久集群,则应等待集群激活。 https://ignite.apache.org/docs/latest/clustering/baseline-topology
  while (!ignite.cluster().active()) {
      System.out.println("Waiting for activation");
      Thread.sleep(5000);
  }


重新连接后,您可能需要重新初始化您的缓存代理。
       cache = ignite.getOrCreateCache(cachecfg); 
}   

1
谢谢回复。我想我希望这一切都可以在后台自动完成,就像我们的数据库或队列连接在服务恢复在线时会“自动工作”一样。 - Sovietaced
我想我可以构建一个抽象层,用逻辑来处理断开连接并重新初始化缓存代理,将所有的ignite缓存方法包装起来。 - Sovietaced

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接