IgniteException:NullPointerException

时间:2017-11-24 16:24:56

标签: spring ignite

我正在使用Ignite 2.2版,我尝试使用以下CacheConfiguration构建一个Cache:

@Configuration
public class IgniteConfig {

    @Bean(name = Identifiers.STANDARD_IGNITE_ITEM)
    @Qualifier(Identifiers.STANDARD_IGNITE_ITEM)
    public CacheConfiguration<String, StandardItem> standardItemsCacheConfiguration() {
        return this.getItemsStandardCacheConfiguration();
    }

    private CacheConfiguration<String, StandardItem> getItemsStandardCacheConfiguration() {
        CacheConfiguration<String, StandardItem> config = new CacheConfiguration<>(Identifiers.STANDARD_IGNITE_ITEM_CACHE);

        config.setName(Identifiers.STANDARD_IGNITE_ITEM_CACHE);
        config.setIndexedTypes(String.class, StandardItem.class);
        config.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        config.setCacheStoreFactory(FactoryBuilder.factoryOf(StandardItemCacheStore.class.getName()));
        config.setReadThrough(true);
        config.setWriteThrough(true);
        config.setCopyOnRead(false);
        config.setCacheMode(CacheMode.PARTITIONED);
        config.setOnheapCacheEnabled(true);

        return config;
    }
}

这是我的CacheStoreAdapter:

public class StandardItemCacheStore extends CacheStoreAdapter<String, StandardItem> {
    private static final Logger logger = LoggerFactory.getLogger(StandardItemCacheStore.class.getName());

    @Override
    public void loadCache(IgniteBiInClosure<String, StandardItem> clo, Object... args) {
        super.loadCache(clo, args);

        logger.info("Load standard items into ingite cache");

        final AtomicInteger counter = new AtomicInteger();

        Flux<StandardItem> items = new StandardItemDAO().getAllItems();

        items.subscribe(item -> {
            if (item != null) {
                counter.incrementAndGet();

                logging.info("Load item into cache: " + item.toString());

                clo.apply(item.getItemId(), item);
            }
        });

        items.doOnComplete(() -> {
           logger.info("Loaded " + counter + " standard items into ignite cache");
        });
    }

    @Override
    public StandardItem load(String key) throws CacheLoaderException {
        return null;
    }

    @Override
    public void write(Cache.Entry<? extends String, ? extends StandardItem> entry) throws CacheWriterException {

    }

    @Override
    public void delete(Object key) throws CacheWriterException {

    }
}

不幸的是我永远无法构建我的缓存,因为有NullPointers,我不明白它们来自哪里。我加载缓存如下:

@Service
public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class.getName());

    private final IgniteCache<String, StandardItem> standardItemIgniteCache;

    public MyService(@Autowired @Qualifier(Identifiers.STANDARD_IGNITE_ITEM) CacheConfiguration<String, StandardItem> standardItemCacheConfiguration) {
        logger.info("Call service provider constructor");

        Ignite ignite = Ignition.getOrStart(new IgniteConfiguration());

        logger.info("Create standard items cache");

        this.standardItemIgniteCache = ignite.getOrCreateCache(standardItemCacheConfiguration);
        this.standardItemIgniteCache.loadCache(null);
    }
}

    // ....
}

当我启动我的服务时,缓存的加载开始(我看到loadCache函数中的项目的logoutput)但突然抛出异常:

2017-11-28 21:23:36.462  INFO 6 --- [           main] i.s.p.p.d.cache.StandardItemCacheStore   : Loaded 35219 standard items into ignite cache
2017-11-28 21:23:36.510 ERROR 6 --- [           main] o.a.i.i.processors.task.GridTaskWorker   : Failed to obtain remote job result policy for result from ComputeTask.result(..) method (will fail the whole task): GridJobResultImpl [job=C2 [c=LoadCacheJobV2 [keepBinary=false]], sib=GridJobSiblingImpl [sesId=26703840061-6c03522c-008e-41f5-b144-a5a08d8c60d2, jobId=36703840061-6c03522c-008e-41f5-b144-a5a08d8c60d2, nodeId=048a70e3-8197-48e0-865f-213828842787, isJobDone=false], jobCtx=GridJobContextImpl [jobId=36703840061-6c03522c-008e-41f5-b144-a5a08d8c60d2, timeoutObj=null, attrs={}], node=TcpDiscoveryNode [id=048a70e3-8197-48e0-865f-213828842787, addrs=[127.0.0.1, 172.19.0.9], sockAddrs=[/127.0.0.1:47500, 7b9717decf77/172.19.0.9:47500], discPort=47500, order=4, intOrder=4, lastExchangeTime=1511904205042, loc=false, ver=2.2.0#20170915-sha1:5747ce6b, isClient=false], ex=class o.a.i.IgniteException: null, hasRes=true, isCancelled=false, isOccupied=true]

org.apache.ignite.IgniteException: Remote job threw user exception (override or implement ComputeTask.result(..) method if you would like to have automatic failover for this exception).
        at org.apache.ignite.compute.ComputeTaskAdapter.result(ComputeTaskAdapter.java:101) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskWorker$5.apply(GridTaskWorker.java:1027) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskWorker$5.apply(GridTaskWorker.java:1020) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.util.IgniteUtils.wrapThreadLoader(IgniteUtils.java:6640) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskWorker.result(GridTaskWorker.java:1020) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskWorker.onResponse(GridTaskWorker.java:838) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskProcessor.processJobExecuteResponse(GridTaskProcessor.java:1066) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.task.GridTaskProcessor$JobMessageListener.onMessage(GridTaskProcessor.java:1301) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.invokeListener(GridIoManager.java:1556) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.processRegularMessage0(GridIoManager.java:1184) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.access$4200(GridIoManager.java:126) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager$9.run(GridIoManager.java:1097) [ignite-core-2.2.0.jar!/:2.2.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: org.apache.ignite.IgniteException: null
        at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2.execute(GridClosureProcessor.java:1850) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.job.GridJobWorker$2.call(GridJobWorker.java:566) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.util.IgniteUtils.wrapThreadLoader(IgniteUtils.java:6608) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.job.GridJobWorker.execute0(GridJobWorker.java:560) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.job.GridJobWorker.body(GridJobWorker.java:489) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.job.GridJobProcessor.processJobExecuteRequest(GridJobProcessor.java:1115) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.job.GridJobProcessor$JobExecutionListener.onMessage(GridJobProcessor.java:1908) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.invokeListener(GridIoManager.java:1556) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.processRegularMessage0(GridIoManager.java:1184) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager.access$4200(GridIoManager.java:126) [ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.managers.communication.GridIoManager$9.run(GridIoManager.java:1097) [ignite-core-2.2.0.jar!/:2.2.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_111]
Caused by: java.lang.NullPointerException: null
        at org.apache.ignite.internal.processors.cache.GridCacheAdapter$LoadCacheJob.localExecute(GridCacheAdapter.java:5456) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.cache.GridCacheAdapter$LoadCacheJobV2.localExecute(GridCacheAdapter.java:5505) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.cache.GridCacheAdapter$TopologyVersionAwareJob.execute(GridCacheAdapter.java:6120) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.compute.ComputeJobAdapter.call(ComputeJobAdapter.java:132) ~[ignite-core-2.2.0.jar!/:2.2.0]
        at org.apache.ignite.internal.processors.closure.GridClosureProcessor$C2.execute(GridClosureProcessor.java:1847) ~[ignite-core-2.2.0.jar!/:2.2.0]
        ... 14 common frames omitted
2017-11-24 16:13:57.319  INFO 6 --- [           main] o.a.i.i.p.r.p.tcp.GridTcpRestProtocol    : Command protocol successfully stopped: TCP binary
2017-11-24 16:13:57.467  INFO 6 --- [           main] o.a.i.i.p.cache.GridCacheProcessor       : Stopped cache [cacheName=ignite-sys-cache]
2017-11-24 16:13:57.468  INFO 6 --- [           main] o.a.i.i.p.cache.GridCacheProcessor       : Stopped cache [cacheName=standard_item_cache]
2017-11-24 16:13:57.469  INFO 6 --- [           main] o.a.i.i.p.cache.GridCacheProcessor       : Stopped cache [cacheName=backend_cache]
[16:13:57] Ignite node stopped OK [uptime=00:00:04:404]
2017-11-24 16:13:57.513  INFO 6 --- [           main] org.apache.ignite.internal.IgniteKernal  :
>>> +---------------------------------------------------------------------------------+
>>> Ignite ver. 2.2.0#20170915-sha1:5747ce6b36f10b30d2142b9d90dad8e3049e8f74 stopped OK
>>> +---------------------------------------------------------------------------------+
>>> Grid uptime: 00:00:04:404

为什么会这样?

1 个答案:

答案 0 :(得分:3)

存在一系列问题导致此类行为:

  1. 您的loadCache方法是异步的。因此,当您调用IgniteCache#loadCache()时,执行不会被阻止,并且几乎立即返回。
  2. 您将Ignite作为try块中的资源启动。因此,当MyService的构造完成时,Ignite实例将关闭。这意味着,整个节点都被破坏了。这就是为什么你看到你的缓存停止了。
  3. 因此,如果您将Ignite创建移出try块,问题就会消失。