-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache Block Location to save memory #16953
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.util.proto; | ||
|
||
import alluxio.collections.IndexDefinition; | ||
import alluxio.collections.IndexedSet; | ||
import alluxio.proto.meta.Block.BlockLocation; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Sets; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* An util class to create cached grpc block locations. | ||
*/ | ||
public class BlockLocationUtils { | ||
private static final IndexDefinition<BlockLocation, BlockLocation> OBJECT_INDEX = | ||
IndexDefinition.ofUnique((b) -> b); | ||
|
||
private static final IndexDefinition<BlockLocation, Long> WORKER_ID_INDEX = | ||
IndexDefinition.ofNonUnique(BlockLocation::getWorkerId); | ||
|
||
// TODO(maobaolong): Add a metric to monitor the size of mLocationCacheMap | ||
private static final IndexedSet<BlockLocation> BLOCK_LOCATION_CACHE = | ||
new IndexedSet<>(OBJECT_INDEX, WORKER_ID_INDEX); | ||
|
||
private static final Set<String> VALID_MEDIUM_TYPE_VALUES = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Athough How about get the set from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll add a comment for this one |
||
Sets.newHashSet("MEM", "HDD", "SSD"); | ||
|
||
/** | ||
* Get a shared grpc block location object. If it does not exist, create and cache it. | ||
* Because the valid values of tierAlias and mediumType are only MEM, SSD and HDD, | ||
* The size of the cache map is limited. | ||
* | ||
* @param workerId the worker id | ||
* @param tierAlias the tier alias | ||
* @param mediumType the medium type | ||
* @return a shared block location object from the cache | ||
*/ | ||
public static BlockLocation getCached( | ||
long workerId, String tierAlias, String mediumType) { | ||
BlockLocation location = BlockLocation | ||
.newBuilder() | ||
.setWorkerId(workerId) | ||
.setTier(tierAlias) | ||
.setMediumType(mediumType) | ||
.build(); | ||
return getCached(location); | ||
} | ||
|
||
/** | ||
* Get a shared grpc block location object. If it does not exist, create and cache it. | ||
* Because the valid values of tierAlias and mediumType are only MEM, SSD and HDD, | ||
* The size of the cache map is limited. | ||
* | ||
* @param blockLocation the block location to cache | ||
* @return a shared block location object from the cache | ||
*/ | ||
public static BlockLocation getCached(BlockLocation blockLocation) { | ||
Preconditions.checkState(VALID_MEDIUM_TYPE_VALUES.contains(blockLocation.getTier()), | ||
"TierAlias must be one of {MEM, HDD and SSD} but got %s", | ||
blockLocation.getTier()); | ||
Preconditions.checkState(VALID_MEDIUM_TYPE_VALUES.contains(blockLocation.getMediumType()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The supported mediumTypes are read from configuration too.
|
||
"MediumType must be one of {MEM, HDD and SSD} but got %s", | ||
blockLocation.getMediumType()); | ||
BLOCK_LOCATION_CACHE.add(blockLocation); | ||
return BLOCK_LOCATION_CACHE.getFirstByField(OBJECT_INDEX, blockLocation); | ||
} | ||
|
||
/** | ||
* Evict cache entries by worker id. | ||
* @param workerId the worker id | ||
*/ | ||
public static void evictByWorkerId(long workerId) { | ||
BLOCK_LOCATION_CACHE.removeByField(WORKER_ID_INDEX, workerId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.util.proto; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotSame; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import alluxio.proto.meta.Block.BlockLocation; | ||
|
||
import org.junit.Test; | ||
|
||
public class BlockLocationUtilsTest { | ||
@Test | ||
public void testBlockLocationCached() { | ||
BlockLocation location1 = BlockLocationUtils.getCached(1, "HDD", "SSD"); | ||
assertEquals("HDD", location1.getTier()); | ||
assertEquals("SSD", location1.getMediumType()); | ||
assertEquals(1, location1.getWorkerId()); | ||
|
||
BlockLocation location2 = BlockLocationUtils.getCached(1, "HDD", "SSD"); | ||
assertSame(location1, location2); | ||
assertEquals(location1, location2); | ||
|
||
BlockLocation location3 = BlockLocationUtils.getCached(location2); | ||
assertSame(location1, location3); | ||
assertEquals(location1, location3); | ||
|
||
BlockLocationUtils.evictByWorkerId(1); | ||
|
||
BlockLocation location4 = BlockLocationUtils.getCached(1, "HDD", "SSD"); | ||
assertNotSame(location1, location4); | ||
assertEquals(location1, location4); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void testInvalidValue() { | ||
BlockLocationUtils.getCached(1, "INVALID", "SSD"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As
mLocationCacheMap
renamed toBLOCK_LOCATION_CACHE
, please update the comment togetherThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure