Skip to content

Commit

Permalink
Merge pull request #3474 from ahmetmircik/fix/3.3/keepLockedKeys
Browse files Browse the repository at this point in the history
Fixes #3473: evictAll should keep locked keys in map (3.3)
  • Loading branch information
mdogan committed Sep 2, 2014
2 parents 12aeeca + e8fe05d commit 8ad6836
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
Expand Up @@ -95,7 +95,7 @@ protected long calculateRecordHeapCost(Record record) {
/**
* Returns total heap cost of collection.
*
* @param collection which's size to be calculated.
* @param collection size to be calculated.
* @return total size of collection.
*/
protected long calculateRecordHeapCost(Collection<Record> collection) {
Expand Down Expand Up @@ -161,6 +161,23 @@ protected void removeIndex(Set<Data> keys) {
}
}

/**
* Removes indexes by excluding keysToPreserve.
*
* @param keysToRemove remove these keys from index.
* @param keysToPreserve do not remove these keys.
*/
protected void removeIndexByPreservingKeys(Set<Data> keysToRemove, Set<Data> keysToPreserve) {
final IndexService indexService = mapContainer.getIndexService();
if (indexService.hasIndex()) {
for (Data key : keysToRemove) {
if (!keysToPreserve.contains(key)) {
indexService.removeEntryIndex(key);
}
}
}
}

protected LockStore createLockStore() {
NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
final LockService lockService = nodeEngine.getSharedService(LockService.SERVICE_NAME);
Expand Down
38 changes: 8 additions & 30 deletions hazelcast/src/main/java/com/hazelcast/map/DefaultRecordStore.java
Expand Up @@ -410,30 +410,20 @@ Object evictInternal(Data key, boolean backup) {
@Override
public int evictAll(boolean backup) {
checkIfLoaded();
final int size = size();
final Set<Data> keysToPreserve = evictAllInternal(backup);
removeIndexByPreservingKeys(keysToPreserve);
return size - keysToPreserve.size();
}

/**
* Internal evict all provides common functionality to all {@link #evictAll(boolean)} ()}
*
* @return preserved keys.
*/
private Set<Data> evictAllInternal(boolean backup) {
final int sizeBeforeEviction = size();
resetSizeEstimator();
resetAccessSequenceNumber();

Set<Data> keysToPreserve = Collections.emptySet();
final Map<Data, Record> recordsToPreserve = getLockedRecords(backup);
if (!recordsToPreserve.isEmpty()) {
keysToPreserve = recordsToPreserve.keySet();
updateSizeEstimator(calculateRecordHeapCost(recordsToPreserve.values()));
}
Map<Data, Record> recordsToPreserve = getLockedRecords(backup);
updateSizeEstimator(calculateRecordHeapCost(recordsToPreserve.values()));

flush(recordsToPreserve, backup);
removeIndexByPreservingKeys(records.keySet(), recordsToPreserve.keySet());
clearRecordsMap(recordsToPreserve);
return keysToPreserve;

final int numberOfEvictedEntries = sizeBeforeEviction - recordsToPreserve.size();
return numberOfEvictedEntries;
}

/**
Expand All @@ -454,18 +444,6 @@ private void flush(Map<Data, Record> excludeRecords, boolean backup) {
}
}

/**
* Removes indexes by excluding keysToPreserve.
*
* @param keysToPreserve should not be removed from index.
*/
private void removeIndexByPreservingKeys(Set<Data> keysToPreserve) {
final Set<Data> currentKeySet = records.keySet();
currentKeySet.removeAll(keysToPreserve);

removeIndex(currentKeySet);
}

/**
* Returns locked records.
*
Expand Down
Expand Up @@ -72,8 +72,12 @@ public void mapEvicted(MapEvent event) {

assertOpenEventually(countDownLatch);
assertEquals(0, countDownLatch.getCount());
assertEquals(numberOfLockedKeys, map.size());
}




@Test
public void testEvictAll_onBackup() throws Exception {
int numberOfEntries = 10000;
Expand Down

0 comments on commit 8ad6836

Please sign in to comment.