Skip to content

Commit

Permalink
Set last-access-time for only LRU records (#18915)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetmircik committed Jun 23, 2021
1 parent b80b074 commit 74a2f5d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.hazelcast.map.impl.mapstore.MapStoreContext;
import com.hazelcast.map.impl.record.Record;
import com.hazelcast.map.impl.record.RecordFactory;
import com.hazelcast.map.impl.record.RecordReaderWriter;
import com.hazelcast.map.impl.record.Records;
import com.hazelcast.spi.impl.NodeEngine;
import com.hazelcast.wan.impl.CallerProvenance;
Expand Down Expand Up @@ -142,7 +143,13 @@ public Record createRecord(Object value, long ttlMillis, long maxIdle, long now)
Record record = recordFactory.newRecord(value);
record.setCreationTime(now);
record.setLastUpdateTime(now);
record.setLastAccessTime(now);
if (record.getMatchingRecordReaderWriter()
== RecordReaderWriter.SIMPLE_DATA_RECORD_WITH_LRU_EVICTION_READER_WRITER) {
// To distinguish last-access-time from creation-time we
// set last-access-time for only LRU records. A LRU record
// has no creation-time field but last-access-time field.
record.setLastAccessTime(now);
}

updateStatsOnPut(false, now);
return record;
Expand Down
94 changes: 91 additions & 3 deletions hazelcast/src/test/java/com/hazelcast/map/ExpirationTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.hazelcast.map;

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.EntryView;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.internal.util.Clock;
import com.hazelcast.map.impl.record.Record;
import com.hazelcast.map.impl.record.RecordReaderWriter;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.TestHazelcastInstanceFactory;
Expand Down Expand Up @@ -365,14 +369,53 @@ public void testExpirationTime_whenMaxIdleTime_isBiggerThan_TTL() {
}

@Test
public void testLastAccessTime_equals_creationTime_afterFirstPut() {
IMap<Integer, Integer> map = createMap();
public void testCreationTime_is_minus_one_when_per_entry_stats_false() {
IMap<Integer, Integer> map = createMapWithLRU(false);
map.put(1, 1);

EntryView<Integer, Integer> entryView = map.getEntryView(1);

assertEquals(-1, entryView.getCreationTime());
}

@Test
public void testLastAccessTime_is_set_at_creation_time__when_per_entry_stats_false() {
long nowBeforePut = getStripedNow();

IMap<Integer, Integer> map = createMapWithLRU(false);
map.put(1, 1);

EntryView<Integer, Integer> entryView = map.getEntryView(1);

assertTrue(entryView.getLastAccessTime() >= nowBeforePut);
}

private static long getStripedNow() {
Record record = newRecord();
record.setLastAccessTime(Clock.currentTimeMillis());
return record.getLastAccessTime();
}

@Test
public void testCreationTime_is_set_when_per_entry_stats_true() {
long nowBeforePut = getStripedNow();

IMap<Integer, Integer> map = createMapWithLRU(true);
map.put(1, 1);

EntryView<Integer, Integer> entryView = map.getEntryView(1);

assertTrue(entryView.getCreationTime() >= nowBeforePut);
}

@Test
public void testLastAccessTime_is_not_set_at_creation_time_when_per_entry_stats_true() {
IMap<Integer, Integer> map = createMapWithLRU(true);
map.put(1, 1);

EntryView<Integer, Integer> entryView = map.getEntryView(1);

assertEquals(entryView.getCreationTime(), entryView.getLastAccessTime());
assertEquals(0, entryView.getLastAccessTime());
}

@Test
Expand Down Expand Up @@ -463,6 +506,17 @@ private <T, U> IMap<T, U> createMap() {
return createHazelcastInstance(config).getMap(mapName);
}

private <T, U> IMap<T, U> createMapWithLRU(boolean perEntryStats) {
String mapName = randomMapName();
Config config = getConfig();
config.getMetricsConfig().setEnabled(false);
MapConfig mapConfig = config.getMapConfig(mapName);
mapConfig.setInMemoryFormat(inMemoryFormat());
mapConfig.setPerEntryStatsEnabled(perEntryStats);
mapConfig.getEvictionConfig().setEvictionPolicy(EvictionPolicy.LRU);
return createHazelcastInstance(config).getMap(mapName);
}

@SuppressWarnings("SameParameterValue")
private IMap<Integer, Integer> createMapWithMaxIdleSeconds(int maxIdleSeconds) {
String mapName = randomMapName();
Expand Down Expand Up @@ -503,4 +557,38 @@ private static long getExpirationTime(IMap<Integer, Integer> map, int key) {
EntryView<Integer, Integer> entryView = map.getEntryView(key);
return entryView.getExpirationTime();
}

private static Record newRecord() {
return new Record() {
@Override
public Object getValue() {
return null;
}

@Override
public void setValue(Object value) {

}

@Override
public long getCost() {
return 0;
}

@Override
public int getVersion() {
return 0;
}

@Override
public void setVersion(int version) {

}

@Override
public RecordReaderWriter getMatchingRecordReaderWriter() {
return null;
}
};
}
}

0 comments on commit 74a2f5d

Please sign in to comment.