如何在重新连接点火服务器后恢复缓存

时间:2017-07-19 22:18:05

标签: ignite

真的很感激,如果有人可以帮助我。

我有一个用Java编写的点火服务器,并且有一个用C#编写的客户端,客户端可以连接到服务器,并且可以正确地获得服务器的缓存。 服务器重新启动后,客户端从服务器收到EVT_CLIENT_NODE_RECONNECTED事件。但是缓存不能再使用了。

服务器代码:

CacheConfiguration cacheConfiguration = new CacheConfiguration();

    cacheConfiguration.setName("Sample");
    cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
    cacheConfiguration.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    cacheConfiguration.setRebalanceMode(CacheRebalanceMode.ASYNC);
    cacheConfiguration.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    cacheConfiguration.setBackups(0);
    cacheConfiguration.setCopyOnRead(true);
    cacheConfiguration.setStoreKeepBinary(false);

    cacheConfiguration.setReadThrough(false);
    cacheConfiguration.setWriteThrough(true);
    cacheConfiguration.setWriteBehindEnabled(true);
    cacheConfiguration.setWriteBehindFlushFrequency(2000);
    cacheConfiguration.setWriteBehindFlushThreadCount(2);

    DriverManagerDataSource theDataSource = new DriverManagerDataSource();
    theDataSource.setDriverClassName("org.postgresql.Driver");
    theDataSource.setUrl("jdbc:postgresql://192.168.224.128:5432/sample");
    theDataSource.setUsername("postgres");
    theDataSource.setPassword("password");


    CacheJdbcPojoStoreFactory jdbcPojoStoreFactory = new CacheJdbcPojoStoreFactory<Long, SampleModel>()
            .setParallelLoadCacheMinimumThreshold(0)
            .setMaximumPoolSize(1)
            .setDataSource(theDataSource);

    cacheConfiguration.setCacheStoreFactory(jdbcPojoStoreFactory);


    Collection<JdbcType> jdbcTypes = new ArrayList<JdbcType>();

    JdbcType jdbcType = new JdbcType();
    jdbcType.setCacheName("Sample");
    jdbcType.setDatabaseSchema("public");
    jdbcType.setKeyType("java.lang.Long");

    Collection<JdbcTypeField> keys = new ArrayList<JdbcTypeField>();
    keys.add(new JdbcTypeField(Types.BIGINT, "id", long.class, "id"));
    jdbcType.setKeyFields(keys.toArray(new JdbcTypeField[keys.size()]));
    Collection<JdbcTypeField> vals = new ArrayList<JdbcTypeField>();

    jdbcType.setDatabaseTable("sample");
    jdbcType.setValueType("com.nmf.SampleModel");

    vals.add(new JdbcTypeField(Types.BIGINT, "id", long.class, "id"));
    vals.add(new JdbcTypeField(Types.VARCHAR, "name", String.class, "name"));

    jdbcType.setValueFields(vals.toArray(new JdbcTypeField[vals.size()]));

    jdbcTypes.add(jdbcType);

    ((CacheJdbcPojoStoreFactory)cacheConfiguration.getCacheStoreFactory()).setTypes(jdbcTypes.toArray(new JdbcType[jdbcTypes.size()]));


    IgniteConfiguration icfg = new IgniteConfiguration();
    icfg.setCacheConfiguration(cacheConfiguration);

    Ignite ignite = Ignition.start(icfg);

的SampleModel:

public class SampleModel implements Serializable {
private long id;
private String Name;

public long getId() {
    return id;
}

public void setId(long id) {
    id = id;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof SampleModel)) return false;

    SampleModel that = (SampleModel) o;

    return id == that.id;
}

@Override
public int hashCode() {
    return (int) (id ^ (id >>> 32));
}

}

客户代码:

ExecutorService executor = Executors.newSingleThreadExecutor(r -> new Thread(r, "worker"));


    CacheConfiguration cacheConfiguration = new CacheConfiguration();

    cacheConfiguration.setName("Sample");
    cacheConfiguration.setCacheMode(CacheMode.REPLICATED);
    cacheConfiguration.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    cacheConfiguration.setRebalanceMode(CacheRebalanceMode.ASYNC);
    cacheConfiguration.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    cacheConfiguration.setBackups(0);
    cacheConfiguration.setCopyOnRead(true);
    cacheConfiguration.setStoreKeepBinary(false);

    IgniteConfiguration icfg = new IgniteConfiguration();
    icfg.setCacheConfiguration(cacheConfiguration);

    icfg.setClientMode(true);

    final Ignite ignite = Ignition.start(icfg);

    ignite.events().localListen(new IgnitePredicate<Event>() {
        public boolean apply(Event event) {
            if (event.type() == EVT_CLIENT_NODE_RECONNECTED) {
                System.out.println("Reconnected");

                executor.submit(()-> {
                    IgniteCache<Long, SampleModel> cache = ignite.getOrCreateCache("Sample");

                    System.out.println("Got the cache");

                    SampleModel model = cache.get(1L);

                    System.out.println(model.getName());
                });
            }

            return true;
        }
    }, EVT_CLIENT_NODE_RECONNECTED);

    IgniteCache<Long, SampleModel> cache = ignite.getOrCreateCache("Sample");

    SampleModel model = cache.get(1L);

    System.out.println(model.getName());

客户端上的错误登录:

    SEVERE: Failed to reinitialize local partitions (preloading will be stopped): GridDhtPartitionExchangeId [topVer=AffinityTopologyVersion [topVer=2, minorTopVer=1], nodeId=dea5f59b, evt=DISCOVERY_CUSTOM_EVT]
class org.apache.ignite.IgniteCheckedException: Failed to start component: class org.apache.ignite.IgniteException: Failed to initialize cache store (data source is not provided).
    at org.apache.ignite.internal.util.IgniteUtils.startLifecycleAware(IgniteUtils.java:8726)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.createCache(GridCacheProcessor.java:1486)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.prepareCacheStart(GridCacheProcessor.java:1931)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.prepareCacheStart(GridCacheProcessor.java:1833)
    at org.apache.ignite.internal.processors.cache.CacheAffinitySharedManager.onCacheChangeRequest(CacheAffinitySharedManager.java:379)
    at org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture.onCacheChangeRequest(GridDhtPartitionsExchangeFuture.java:688)
    at org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture.init(GridDhtPartitionsExchangeFuture.java:529)
    at org.apache.ignite.internal.processors.cache.GridCachePartitionExchangeManager$ExchangeWorker.body(GridCachePartitionExchangeManager.java:1806)
    at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110)
    at java.lang.Thread.run(Thread.java:745)
Caused by: class org.apache.ignite.IgniteException: Failed to initialize cache store (data source is not provided).
    at org.apache.ignite.cache.store.jdbc.CacheAbstractJdbcStore.start(CacheAbstractJdbcStore.java:298)
    at org.apache.ignite.internal.util.IgniteUtils.startLifecycleAware(IgniteUtils.java:8722)
    ... 9 more

July 25, 2017 12:58:38 PM org.apache.ignite.logger.java.JavaLogger error
SEVERE: Failed to wait for completion of partition map exchange (preloading will not start): GridDhtPartitionsExchangeFuture [dummy=false, forcePreload=false, reassign=false, discoEvt=DiscoveryCustomEvent [customMsg=null, affTopVer=AffinityTopologyVersion [topVer=2, minorTopVer=1], super=DiscoveryEvent [evtNode=TcpDiscoveryNode [id=dea5f59b-bdda-47a1-b31d-1ecb08fc746f, addrs=[0:0:0:0:0:0:0:1, 127.0.0.1, 192.168.224.1, 192.168.6.15, 192.168.80.1, 2001:0:9d38:90d7:c83:fac:98d7:5fc1], sockAddrs=[/0:0:0:0:0:0:0:1:0, /127.0.0.1:0, Ares-W11/169.254.194.93:0, /2001:0:9d38:90d7:c83:fac:98d7:5fc1:0, /192.168.6.15:0, windows10.microdone.cn/192.168.224.1:0, /192.168.80.1:0], discPort=0, order=2, intOrder=0, lastExchangeTime=1500958697559, loc=true, ver=2.0.0#20170430-sha1:d4eef3c6, isClient=true], topVer=2, nodeId8=dea5f59b, msg=null, type=DISCOVERY_CUSTOM_EVT, tstamp=1500958718133]], crd=TcpDiscoveryNode [id=247d2926-010d-429b-aef2-97a18fbb3b5d, addrs=[0:0:0:0:0:0:0:1, 127.0.0.1, 192.168.224.1, 192.168.6.15, 192.168.80.1, 2001:0:9d38:90d7:c83:fac:98d7:5fc1], sockAddrs=[/192.168.6.15:47500, /2001:0:9d38:90d7:c83:fac:98d7:5fc1:47500, windows10.microdone.cn/192.168.224.1:47500, /192.168.80.1:47500, Ares-W11/169.254.194.93:47500, /0:0:0:0:0:0:0:1:47500, /127.0.0.1:47500], discPort=47500, order=1, intOrder=1, lastExchangeTime=1500958718083, loc=false, ver=2.0.0#20170430-sha1:d4eef3c6, isClient=false], exchId=GridDhtPartitionExchangeId [topVer=AffinityTopologyVersion [topVer=2, minorTopVer=1], nodeId=dea5f59b, evt=DISCOVERY_CUSTOM_EVT], added=true, initFut=GridFutureAdapter [ignoreInterrupts=false, state=DONE, res=false, hash=842035444], init=false, lastVer=null, partReleaseFut=null, affChangeMsg=null, skipPreload=true, clientOnlyExchange=false, initTs=1500958718133, centralizedAff=false, changeGlobalStateE=null, exchangeOnChangeGlobalState=false, forcedRebFut=null, evtLatch=0, remaining=[247d2926-010d-429b-aef2-97a18fbb3b5d], srvNodes=[TcpDiscoveryNode [id=247d2926-010d-429b-aef2-97a18fbb3b5d, addrs=[0:0:0:0:0:0:0:1, 127.0.0.1, 192.168.224.1, 192.168.6.15, 192.168.80.1, 2001:0:9d38:90d7:c83:fac:98d7:5fc1], sockAddrs=[/192.168.6.15:47500, /2001:0:9d38:90d7:c83:fac:98d7:5fc1:47500, windows10.microdone.cn/192.168.224.1:47500, /192.168.80.1:47500, Ares-W11/169.254.194.93:47500, /0:0:0:0:0:0:0:1:47500, /127.0.0.1:47500], discPort=47500, order=1, intOrder=1, lastExchangeTime=1500958718083, loc=false, ver=2.0.0#20170430-sha1:d4eef3c6, isClient=false]], super=GridFutureAdapter [ignoreInterrupts=false, state=DONE, res=class o.a.i.IgniteCheckedException: Failed to start component: class o.a.i.IgniteException: Failed to initialize cache store (data source is not provided)., hash=1281081640]]
class org.apache.ignite.IgniteCheckedException: Failed to start component: class org.apache.ignite.IgniteException: Failed to initialize cache store (data source is not provided).
    at org.apache.ignite.internal.util.IgniteUtils.startLifecycleAware(IgniteUtils.java:8726)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.createCache(GridCacheProcessor.java:1486)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.prepareCacheStart(GridCacheProcessor.java:1931)
    at org.apache.ignite.internal.processors.cache.GridCacheProcessor.prepareCacheStart(GridCacheProcessor.java:1833)
    at org.apache.ignite.internal.processors.cache.CacheAffinitySharedManager.onCacheChangeRequest(CacheAffinitySharedManager.java:379)
    at org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture.onCacheChangeRequest(GridDhtPartitionsExchangeFuture.java:688)
    at org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture.init(GridDhtPartitionsExchangeFuture.java:529)
    at org.apache.ignite.internal.processors.cache.GridCachePartitionExchangeManager$ExchangeWorker.body(GridCachePartitionExchangeManager.java:1806)
    at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110)
    at java.lang.Thread.run(Thread.java:745)
Caused by: class org.apache.ignite.IgniteException: Failed to initialize cache store (data source is not provided).
    at org.apache.ignite.cache.store.jdbc.CacheAbstractJdbcStore.start(CacheAbstractJdbcStore.java:298)
    at org.apache.ignite.internal.util.IgniteUtils.startLifecycleAware(IgniteUtils.java:8722)
    ... 9 more

2 个答案:

答案 0 :(得分:0)

只有服务器节点存储缓存(本地缓存除外),因此,当您重新启动服务器节点时,此缓存已停止。这里的问题是客户端节点重新连接到群集,但没有将其作为新节点加入。这就是为什么不再创建缓存的原因。

我认为它的错误行为和缓存应该在客户端重新连接时重新创建。 我为此创建了issue

作为一种解决方法,您可以使用Ignite.GetOrCreateCache(&#34; Sample&#34;)方法代替Ignite.GetCache(&#34; Sample&#34;)

答案 1 :(得分:0)

您是否仍然遇到Ignite.GetOrCreateCache(&#34; Sample&#34;)挂起的问题?确保您没有从系统池中的某个线程进行该调用。当我遇到类似问题时,我正在侦听EVT_CLIENT_NODE_RECONNECTED事件并调用Ignite.GetOrCreateCache(&#34; Sample&#34;)。有关详细信息,请参阅此问题的答案:Closures stuck in 2.0 when try to add an element into the queue