/
EvictionPolicyEvaluatorTest.java
224 lines (186 loc) · 7.82 KB
/
EvictionPolicyEvaluatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.eviction;
import com.hazelcast.cache.impl.record.CacheObjectRecord;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator;
import com.hazelcast.spi.eviction.EvictionPolicyComparator;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
import static com.hazelcast.internal.eviction.EvictionPolicyEvaluatorProvider.getEvictionPolicyEvaluator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class EvictionPolicyEvaluatorTest extends HazelcastTestSupport {
private final class SimpleEvictionCandidate<K, V extends Evictable> implements EvictionCandidate<K, V> {
private K key;
private V value;
private SimpleEvictionCandidate(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getAccessor() {
return key;
}
@Override
public V getEvictable() {
return value;
}
@Override
public Object getKey() {
throw new UnsupportedOperationException();
}
@Override
public Object getValue() {
throw new UnsupportedOperationException();
}
@Override
public long getCreationTime() {
return getEvictable().getCreationTime();
}
@Override
public long getLastAccessTime() {
return getEvictable().getLastAccessTime();
}
@Override
public long getHits() {
return getEvictable().getHits();
}
}
@Test
public void test_leastRecentlyAccessedEntry_isSelected_when_evictionPolicy_is_LRU() {
test_evictionPolicyLRU(false);
}
@Test
public void test_expiredEntry_hasMorePriority_than_leastRecentlyAccessedEntry_toBeEvicted_when_evictionPolicy_is_LRU() {
test_evictionPolicyLRU(true);
}
private void test_evictionPolicyLRU(boolean useExpiredEntry) {
final int recordCount = 100;
final int expectedEvictedRecordValue = recordCount / 2;
final int expectedExpiredRecordValue = useExpiredEntry ? recordCount / 4 : -1;
EvictionConfiguration evictionConfig = new EvictionConfiguration() {
@Override
public EvictionStrategyType getEvictionStrategyType() {
return null;
}
@Override
public EvictionPolicy getEvictionPolicy() {
return EvictionPolicy.LRU;
}
@Override
public String getComparatorClassName() {
return null;
}
@Override
public EvictionPolicyComparator getComparator() {
return null;
}
};
EvictionPolicyEvaluator evictionPolicyEvaluator = getEvictionPolicyEvaluator(evictionConfig, null);
List<EvictionCandidate<Integer, CacheObjectRecord>> records
= new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
long baseTime = System.currentTimeMillis();
for (int i = 0; i < recordCount; i++) {
long creationTime = baseTime + (i * 100);
CacheObjectRecord record = new CacheObjectRecord(i, creationTime, Long.MAX_VALUE);
if (i == expectedEvictedRecordValue) {
// The record in the middle will be minimum access time.
// So, it will be selected for eviction
record.setLastAccessTime(baseTime - 1000);
} else if (i == expectedExpiredRecordValue) {
record.setExpirationTime(System.currentTimeMillis());
} else {
record.setLastAccessTime(creationTime + 1000);
}
records.add(new SimpleEvictionCandidate<Integer, CacheObjectRecord>(i, record));
}
sleepAtLeastMillis(1);
EvictionCandidate<Integer, CacheObjectRecord> evictionCandidate = evictionPolicyEvaluator.evaluate(records);
assertNotNull(evictionCandidate);
CacheObjectRecord evictedRecord = evictionCandidate.getEvictable();
assertNotNull(evictedRecord);
if (useExpiredEntry) {
assertEquals(expectedExpiredRecordValue, evictedRecord.getValue());
} else {
assertEquals(expectedEvictedRecordValue, evictedRecord.getValue());
}
}
@Test
public void test_leastFrequentlyUsedEntry_isSelected_when_evictionPolicy_is_LFU() {
test_evictionPolicyLFU(false);
}
@Test
public void test_expiredEntry_hasMorePriority_than_leastFrequentlyUsedEntry_toBeEvicted_when_evictionPolicy_is_LFU() {
test_evictionPolicyLFU(true);
}
private void test_evictionPolicyLFU(boolean useExpiredEntry) {
final int recordCount = 100;
final int expectedEvictedRecordValue = recordCount / 2;
final int expectedExpiredRecordValue = useExpiredEntry ? recordCount / 4 : -1;
EvictionConfiguration evictionConfig = new EvictionConfiguration() {
@Override
public EvictionStrategyType getEvictionStrategyType() {
return null;
}
@Override
public EvictionPolicy getEvictionPolicy() {
return EvictionPolicy.LFU;
}
@Override
public String getComparatorClassName() {
return null;
}
@Override
public EvictionPolicyComparator getComparator() {
return null;
}
};
EvictionPolicyEvaluator evictionPolicyEvaluator = getEvictionPolicyEvaluator(evictionConfig, null);
List<EvictionCandidate<Integer, CacheObjectRecord>> records
= new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
for (int i = 0; i < recordCount; i++) {
CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
if (i == expectedEvictedRecordValue) {
// The record in the middle will be minimum access hit.
// So, it will be selected for eviction
record.setHits(0);
} else if (i == expectedExpiredRecordValue) {
record.setExpirationTime(System.currentTimeMillis());
} else {
record.setHits(i + 1);
}
records.add(new SimpleEvictionCandidate<>(i, record));
}
EvictionCandidate<Integer, CacheObjectRecord> evictionCandidate = evictionPolicyEvaluator.evaluate(records);
assertNotNull(evictionCandidate);
CacheObjectRecord evictedRecord = evictionCandidate.getEvictable();
assertNotNull(evictedRecord);
if (useExpiredEntry) {
assertEquals(expectedExpiredRecordValue, evictedRecord.getValue());
} else {
assertEquals(expectedEvictedRecordValue, evictedRecord.getValue());
}
}
}