Search code examples
javaspring-bootspring-dataignitegridgain

Apache Ignite - Expiry Policy not working when Native Persistence Enabled


I am using Apache Ignite with Spring Data in Persistent Storage mode (ignite version 2.9.1 with spring data 2.2). When I set the Expiry Policy I noticed that the objects get not removed automatically. It works when I turn off the native persistence.

Any clue what I missed?

Here the specification from GridGain Link

Update 1: It looks like to be dependent if you have already an existing native ignite DB or not i.e. When I delete all the files from the Ignite DB then it is working. But If I then try to change the policy, it gets not recognized when I restart the Ignite cluster. It still continues to use the previous policy when the DB files has been first time created.

Any thoughts?

Configuration Class

    @Bean
    public IgniteConfiguration igniteConfig() {
        
        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
        // Enabling peer-class loading feature.
        igniteConfiguration.setPeerClassLoadingEnabled(true);
        igniteConfiguration.setClientMode(false);
        // Data Storage
        DataStorageConfiguration dataStorageConfiguration = new DataStorageConfiguration();
        dataStorageConfiguration.setPageSize(4 * 1024);
        // Data Region
        DataRegionConfiguration dataRegionConfiguration = new DataRegionConfiguration();
        dataRegionConfiguration.setName(DATA_CONFIG_NAME);
        dataRegionConfiguration.setInitialSize(100 * 1000 * 1000);
        dataRegionConfiguration.setMaxSize(200 * 1000 * 1000);
        
        /// enabling natvie persisten <-- After enabling the expiry policy is not Working !!!
        dataRegionConfiguration.setPersistenceEnabled(true);
        dataStorageConfiguration.setDataRegionConfigurations(dataRegionConfiguration);
        igniteConfiguration.setDataStorageConfiguration(dataStorageConfiguration);
        igniteConfiguration.setConsistentId("ItemFileSystem");
        
        CacheConfiguration<Long,Item> itemCache=new CacheConfiguration<Long,Item>();
        itemCache.setCopyOnRead(false);
        // as we have one node for now
        itemCache.setBackups(1);
        itemCache.setAtomicityMode(CacheAtomicityMode.ATOMIC);
        itemCache.setName(Item.cacheName);
        itemCache.setDataRegionName(DATA_CONFIG_NAME);
        itemCache.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
        itemCache.setIndexedTypes(Long.class,Item.class);   

        // setting up the Expiry Policy
        itemCache.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 30)));
        itemCache.setEagerTtl(true);
        
        igniteConfiguration.setCacheConfiguration(itemCache);

        return igniteConfiguration;
    }
    
    @Bean(name = "igniteInstance",destroyMethod = "close")
    public Ignite ignite(IgniteConfiguration igniteConfiguration) throws IgniteException {
        final Ignite ignite = Ignition.start(igniteConfiguration);
        ignite.cluster().state(ClusterState.ACTIVE);
        return ignite;
    }

Main program

    @Autowired
    private ItemIgniteRepository itemRepo;

    @Override
    public void run(String... args) throws Exception {
        log.info("XXXXXXXXXXX  application started... XXXXXXXXXXX");

        // delete everything in memory and native persistence       
        itemRepo.deleteAll();

        Item item = new Item("AIX-1","Advanced Xtra Item");
        itemRepo.save(item.getId(),item);

        // start a thread which will print every 10s the items from cache
        ListItemsThread target = new ListItemsThread();
        Thread t = new Thread(target);
        t.start();
        log.info("waiting for shutdown...");
        System.in.read();
        target.keepRunning = false;
        log.info("XXXXXXXXXXX  application finished... XXXXXXXXXXX");
        applicationContext.close();
    }

    class ListItemsThread implements Runnable {

        public boolean keepRunning = true;
        
        @Override
        public void run() {
             while(keepRunning) {
                Long itemsCount = itemRepo.count();
                log.info("Number of Items: {}", itemsCount);
                Iterable<Item>items = itemRepo.findAll();
                for(Item item : items) {
                    log.info("item: {}", item);
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
        }
    }




Solution

  • Cache definitions -- including the expiry policy -- are immutable and cannot be changed after creation. With a purely in-memory cluster, that happens when you restart the cluster. With persistence, you have to drop and re-create the table.