java - Apache Ignite:重新连接到 Ignite 服务器后缓存无法使用
问题描述
我正在使用 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 服务器并在服务器在线后继续工作。从我读过的内容来看,胖客户端应该这样做,但我没有看到这种情况发生。为什么缓存仍然被认为是停止的?
我们使用 Ignite 2.7.6 和 Kubernetes IP finder。
解决方案
看起来您正在使用陈旧的缓存代理。
如果您使用的是内存集群,并从客户端动态创建了缓存,那么当集群重新启动时,给定的缓存将消失。
以下代码从客户端针对内存中的集群执行,如果所讨论的缓存不是服务器配置的一部分,而是在客户端上动态创建的,则会在集群重新启动时生成异常。
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);
}