use of com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator in project hazelcast by hazelcast.
the class EvictionPolicyEvaluatorProvider method getEvictionPolicyEvaluator.
/**
* Gets the {@link EvictionPolicyEvaluator} implementation specified with {@code evictionPolicy}.
*
* @param evictionConfig {@link EvictionConfiguration} for requested {@link EvictionPolicyEvaluator} implementation
* @param classLoader the {@link java.lang.ClassLoader} to be used
* while creating custom {@link EvictionPolicyComparator} if it is specified in the config
* @return the requested {@link EvictionPolicyEvaluator} implementation
*/
public static <A, E extends Evictable> EvictionPolicyEvaluator<A, E> getEvictionPolicyEvaluator(EvictionConfiguration evictionConfig, ClassLoader classLoader) {
if (evictionConfig == null) {
return null;
}
EvictionPolicyComparator evictionPolicyComparator;
String evictionPolicyComparatorClassName = evictionConfig.getComparatorClassName();
if (!StringUtil.isNullOrEmpty(evictionPolicyComparatorClassName)) {
try {
evictionPolicyComparator = ClassLoaderUtil.newInstance(classLoader, evictionPolicyComparatorClassName);
} catch (Exception e) {
throw rethrow(e);
}
} else {
EvictionPolicyComparator comparator = evictionConfig.getComparator();
if (comparator != null) {
evictionPolicyComparator = comparator;
} else {
EvictionPolicyType evictionPolicyType = evictionConfig.getEvictionPolicyType();
if (evictionPolicyType == null) {
return null;
}
evictionPolicyComparator = createEvictionPolicyComparator(evictionPolicyType);
}
}
return new EvictionPolicyEvaluator<A, E>(evictionPolicyComparator);
}
use of com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator in project hazelcast by hazelcast.
the class EvictionPolicyEvaluatorTest method test_evictionPolicyLRU.
private void test_evictionPolicyLRU(boolean useExpiredEntry) {
final int RECORD_COUNT = 100;
final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
final int EXPECTED_EXPIRED_RECORD_VALUE = useExpiredEntry ? RECORD_COUNT / 4 : -1;
EvictionConfiguration evictionConfig = new EvictionConfiguration() {
@Override
public EvictionStrategyType getEvictionStrategyType() {
return null;
}
@Override
public EvictionPolicyType getEvictionPolicyType() {
return EvictionPolicyType.LRU;
}
@Override
public String getComparatorClassName() {
return null;
}
@Override
public EvictionPolicyComparator getComparator() {
return null;
}
};
EvictionPolicyEvaluator evictionPolicyEvaluator = EvictionPolicyEvaluatorProvider.getEvictionPolicyEvaluator(evictionConfig, null);
List<EvictionCandidate<Integer, CacheObjectRecord>> records = new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
long baseTime = System.currentTimeMillis();
for (int i = 0; i < RECORD_COUNT; i++) {
long creationTime = baseTime + (i * 100);
CacheObjectRecord record = new CacheObjectRecord(i, creationTime, Long.MAX_VALUE);
if (i == EXPECTED_EVICTED_RECORD_VALUE) {
// The record in the middle will be minimum access time.
// So, it will be selected for eviction
record.setAccessTime(baseTime - 1000);
} else if (i == EXPECTED_EXPIRED_RECORD_VALUE) {
record.setExpirationTime(System.currentTimeMillis());
} else {
record.setAccessTime(creationTime + 1000);
}
records.add(new SimpleEvictionCandidate<Integer, CacheObjectRecord>(i, record));
}
sleepAtLeastMillis(1);
Iterable<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecords = evictionPolicyEvaluator.evaluate(records);
assertNotNull(evictedRecords);
Iterator<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecordsIterator = evictedRecords.iterator();
assertTrue(evictedRecordsIterator.hasNext());
EvictionCandidate<Integer, CacheObjectRecord> candidateEvictedRecord = evictedRecordsIterator.next();
assertNotNull(candidateEvictedRecord);
assertFalse(evictedRecordsIterator.hasNext());
CacheObjectRecord evictedRecord = candidateEvictedRecord.getEvictable();
assertNotNull(evictedRecord);
if (useExpiredEntry) {
assertEquals(EXPECTED_EXPIRED_RECORD_VALUE, evictedRecord.getValue());
} else {
assertEquals(EXPECTED_EVICTED_RECORD_VALUE, evictedRecord.getValue());
}
}
use of com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator in project hazelcast by hazelcast.
the class EvictionPolicyEvaluatorTest method test_evictionPolicyLFU.
private void test_evictionPolicyLFU(boolean useExpiredEntry) {
final int RECORD_COUNT = 100;
final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
final int EXPECTED_EXPIRED_RECORD_VALUE = useExpiredEntry ? RECORD_COUNT / 4 : -1;
EvictionConfiguration evictionConfig = new EvictionConfiguration() {
@Override
public EvictionStrategyType getEvictionStrategyType() {
return null;
}
@Override
public EvictionPolicyType getEvictionPolicyType() {
return EvictionPolicyType.LFU;
}
@Override
public String getComparatorClassName() {
return null;
}
@Override
public EvictionPolicyComparator getComparator() {
return null;
}
};
EvictionPolicyEvaluator evictionPolicyEvaluator = EvictionPolicyEvaluatorProvider.getEvictionPolicyEvaluator(evictionConfig, null);
List<EvictionCandidate<Integer, CacheObjectRecord>> records = new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
for (int i = 0; i < RECORD_COUNT; i++) {
CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
if (i == EXPECTED_EVICTED_RECORD_VALUE) {
// The record in the middle will be minimum access hit.
// So, it will be selected for eviction
record.setAccessHit(0);
} else if (i == EXPECTED_EXPIRED_RECORD_VALUE) {
record.setExpirationTime(System.currentTimeMillis());
} else {
record.setAccessHit(i + 1);
}
records.add(new SimpleEvictionCandidate<Integer, CacheObjectRecord>(i, record));
}
Iterable<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecords = evictionPolicyEvaluator.evaluate(records);
assertNotNull(evictedRecords);
Iterator<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecordsIterator = evictedRecords.iterator();
assertTrue(evictedRecordsIterator.hasNext());
EvictionCandidate<Integer, CacheObjectRecord> candidateEvictedRecord = evictedRecordsIterator.next();
assertNotNull(candidateEvictedRecord);
assertFalse(evictedRecordsIterator.hasNext());
CacheObjectRecord evictedRecord = candidateEvictedRecord.getEvictable();
assertNotNull(evictedRecord);
if (useExpiredEntry) {
assertEquals(EXPECTED_EXPIRED_RECORD_VALUE, evictedRecord.getValue());
} else {
assertEquals(EXPECTED_EVICTED_RECORD_VALUE, evictedRecord.getValue());
}
}
use of com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator in project hazelcast by hazelcast.
the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.
@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
final int RECORD_COUNT = 100;
final int EXPECTED_EVICTED_COUNT = 1;
final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
Node node = TestUtil.getNode(instance);
SerializationService serializationService = node.getSerializationService();
ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
EvictionConfiguration evictionConfig = new EvictionConfiguration() {
@Override
public EvictionStrategyType getEvictionStrategyType() {
return EvictionStrategyType.SAMPLING_BASED_EVICTION;
}
@Override
public EvictionPolicyType getEvictionPolicyType() {
return null;
}
@Override
public String getComparatorClassName() {
return null;
}
@Override
public EvictionPolicyComparator getComparator() {
return null;
}
};
SamplingEvictionStrategy<K, V, S> evictionStrategy = SamplingEvictionStrategy.INSTANCE;
CacheRecordHashMap cacheRecordMap = new CacheRecordHashMap(serializationService, 1000, cacheContext);
CacheObjectRecord expectedEvictedRecord = null;
Data expectedData = null;
for (int i = 0; i < RECORD_COUNT; i++) {
CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
Data data = serializationService.toData(i);
cacheRecordMap.put(data, record);
if (i == EXPECTED_EVICTED_RECORD_VALUE) {
expectedEvictedRecord = record;
expectedData = data;
}
}
assertNotNull(expectedEvictedRecord);
assertNotNull(expectedData);
final SimpleEvictionCandidate evictionCandidate = new SimpleEvictionCandidate((K) expectedData, (V) expectedEvictedRecord);
// we are testing "EvictionStrategy" in this test, so we mock "EvictionPolicyEvaluator" (it's tested in another test)
EvictionPolicyEvaluator evictionPolicyEvaluator = mock(EvictionPolicyEvaluator.class);
when(evictionPolicyEvaluator.evaluate(Matchers.any(Iterable.class))).thenReturn(Collections.singleton(evictionCandidate));
when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
assertEquals(RECORD_COUNT, cacheRecordMap.size());
assertTrue(cacheRecordMap.containsKey(expectedData));
assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
int evictedCount = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
assertEquals(EXPECTED_EVICTED_COUNT, evictedCount);
assertEquals(RECORD_COUNT - EXPECTED_EVICTED_COUNT, cacheRecordMap.size());
assertFalse(cacheRecordMap.containsKey(expectedData));
assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
Aggregations