hazelcast / hazelcast

Hazelcast is a unified real-time data platform combining stream processing with a fast data store, allowing customers to act instantly on data-in-motion for real-time insights.
https://www.hazelcast.com
Other
6.14k stars 1.84k forks source link

Hazelcast 3.3.2 - time based eviction #5014

Closed RovoMe closed 9 years ago

RovoMe commented 9 years ago

As we are currently experimenting with Hazelcast for its applicability as a caching solution for a leaky-bucket filter, which relies on the eviction of entries, we noticed some inconsitencies with the timing behavior when entries actually evict.

As there might be different eviction time-frames for different clients (not sure if we are really going in that direction) we make use of put(K, V, long, TimeUnit). To test the eviction behavior of entries using this method we came up with this simple unit test:

@Test
public void testEvictionOnkeyUpdate() throws Exception {
    String testMap = "testMap";
    HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
    HazelcastInstance hzClient = HazelcastClient.newHazelcastClient();

    IMap<String, String> map = hzClient.getMap(testMap);

    // # 1: does an entry evict correctly?
    map.put("key", "test", 1L, TimeUnit.SECONDS);
    assertThat(map.size(), is(equalTo(1)));

    Thread.sleep(900L);

    assertThat(map.size(), is(equalTo(1)));

    Thread.sleep(900L);

    assertThat(map.size(), is(equalTo(0)));

    // # 2: when does an entry evict if it is updated?
    map.put("key", "test", 1L, TimeUnit.SECONDS);

    Thread.sleep(900L);

    assertThat(map.size(), is(equalTo(1)));

    map.put("key", "test", 1L, TimeUnit.SECONDS);

    Thread.sleep(900L);

    assertThat(map.size(), is(equalTo(1)));

    // Thread.sleep(900L); // assert below FAILS!
    // Thread.sleep(2500L); // FAILS also!
    Thread.sleep(3000L); // SUCCEEDS

    assertThat(map.size(), is(equalTo(0)));

    HazelcastClient.shutdownAll();
    Hazelcast.shutdownAll();
}

There is no further configuration defined for this test (INFORMATION: Loading 'hazelcast-default.xml' from classpath.) and the unit-test is the only Hazelcast instance (Members [1] { Member [192.168.1.113]:5701 this }). All except the last assert work correctly. The last assert only succeeds if the Thread.sleep(...) is set to three seconds in my particluar case.

Is this behavior expected? Is there any other way to define client-specific time-outs of entries which do evict almost immediately after the specified TTL?

ahmetmircik commented 9 years ago

Hi Roman, It is expected behavior, please read the comment here. Expired entries can not be reached but when you check the size you may see that map still has some entries to be swept but eventually that size should come to zero. You can also check that behavior by using eventual assertions like this