Skip to content

Commit c2385c0

Browse files
HDFS-16882. RBF: Add cache hit rate metric in MountTableResolver#getDestinationForPath (#5276) (#5423). Contributed by farmmamba
Reviewed-by: Inigo Goiri <[email protected]> Reviewed-by: Tao Li <[email protected]> Signed-off-by: Ayush Saxena <[email protected]>
1 parent 19a6762 commit c2385c0

4 files changed

Lines changed: 73 additions & 4 deletions

File tree

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/metrics/StateStoreMetrics.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
3131
import org.apache.hadoop.metrics2.lib.MetricsRegistry;
3232
import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
33+
import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
3334
import org.apache.hadoop.metrics2.lib.MutableRate;
3435

3536
import org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting;
@@ -136,6 +137,19 @@ public void setCacheSize(String name, int size) {
136137
counter.set(size);
137138
}
138139

140+
/**
141+
* set the count of the location cache access information.
142+
* @param name Name of the record.
143+
* @param count count of the record.
144+
*/
145+
public void setLocationCache(String name, long count) {
146+
MutableGaugeLong counter = (MutableGaugeLong) registry.get(name);
147+
if (counter == null) {
148+
counter = registry.newGauge(name, name, count);
149+
}
150+
counter.set(count);
151+
}
152+
139153
@VisibleForTesting
140154
public void reset() {
141155
reads.resetMinMax();

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.concurrent.ConcurrentHashMap;
4343
import java.util.concurrent.ConcurrentMap;
4444
import java.util.concurrent.ExecutionException;
45+
import java.util.concurrent.atomic.LongAdder;
4546
import java.util.concurrent.locks.Lock;
4647
import java.util.concurrent.locks.ReadWriteLock;
4748
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -51,6 +52,7 @@
5152
import org.apache.hadoop.conf.Configuration;
5253
import org.apache.hadoop.fs.Path;
5354
import org.apache.hadoop.fs.FileSystem;
55+
import org.apache.hadoop.hdfs.server.federation.metrics.StateStoreMetrics;
5456
import org.apache.hadoop.hdfs.server.federation.resolver.order.DestinationOrder;
5557
import org.apache.hadoop.hdfs.server.federation.router.Router;
5658
import org.apache.hadoop.hdfs.server.federation.router.RouterRpcServer;
@@ -97,6 +99,8 @@ public class MountTableResolver
9799
private final TreeMap<String, MountTable> tree = new TreeMap<>();
98100
/** Path -> Remote location. */
99101
private final Cache<String, PathLocation> locationCache;
102+
private final LongAdder locCacheMiss = new LongAdder();
103+
private final LongAdder locCacheAccess = new LongAdder();
100104

101105
/** Default nameservice when no mount matches the math. */
102106
private String defaultNameService = "";
@@ -408,6 +412,9 @@ public boolean loadCache(boolean force) {
408412
mountTable.getMountTableEntries(request);
409413
List<MountTable> records = response.getEntries();
410414
refreshEntries(records);
415+
StateStoreMetrics metrics = this.getMountTableStore().getDriver().getMetrics();
416+
metrics.setLocationCache("locationCacheMissed", this.getLocCacheMiss().sum());
417+
metrics.setLocationCache("locationCacheAccessed", this.getLocCacheAccess().sum());
411418
} catch (IOException e) {
412419
LOG.error("Cannot fetch mount table entries from State Store", e);
413420
return false;
@@ -441,9 +448,12 @@ public PathLocation getDestinationForPath(final String path)
441448
if (this.locationCache == null) {
442449
res = lookupLocation(processTrashPath(path));
443450
} else {
444-
Callable<? extends PathLocation> meh = (Callable<PathLocation>) () ->
445-
lookupLocation(processTrashPath(path));
451+
Callable<? extends PathLocation> meh = (Callable<PathLocation>) () -> {
452+
this.getLocCacheMiss().increment();
453+
return lookupLocation(processTrashPath(path));
454+
};
446455
res = this.locationCache.get(processTrashPath(path), meh);
456+
this.getLocCacheAccess().increment();
447457
}
448458
if (isTrashPath(path)) {
449459
List<RemoteLocation> remoteLocations = new ArrayList<>();
@@ -734,4 +744,12 @@ public void setDefaultNSEnable(boolean defaultNSRWEnable) {
734744
public void setDisabled(boolean disable) {
735745
this.disabled = disable;
736746
}
747+
748+
public LongAdder getLocCacheMiss() {
749+
return locCacheMiss;
750+
}
751+
752+
public LongAdder getLocCacheAccess() {
753+
return locCacheAccess;
754+
}
737755
}

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/store/RecordStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Class<R> getRecordClass() {
7373
*
7474
* @return State Store driver.
7575
*/
76-
protected StateStoreDriver getDriver() {
76+
public StateStoreDriver getDriver() {
7777
return this.driver;
7878
}
7979

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestMountTableResolver.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,4 +729,41 @@ public void testInvalidateCache() throws Exception {
729729
assertEquals("2->/testInvalidateCache/foo", mountTable
730730
.getDestinationForPath("/testInvalidateCache/foo").toString());
731731
}
732-
}
732+
733+
/**
734+
* Test location cache hit when get destination for path.
735+
*/
736+
@Test
737+
public void testLocationCacheHitrate() throws Exception {
738+
List<MountTable> entries = new ArrayList<>();
739+
740+
// Add entry and test location cache
741+
Map<String, String> map1 = getMountTableEntry("1", "/testlocationcache");
742+
MountTable entry1 = MountTable.newInstance("/testlocationcache", map1);
743+
entries.add(entry1);
744+
745+
Map<String, String> map2 = getMountTableEntry("2",
746+
"/anothertestlocationcache");
747+
MountTable entry2 = MountTable.newInstance("/anothertestlocationcache",
748+
map2);
749+
entries.add(entry2);
750+
751+
mountTable.refreshEntries(entries);
752+
mountTable.getLocCacheAccess().reset();
753+
mountTable.getLocCacheMiss().reset();
754+
assertEquals("1->/testlocationcache",
755+
mountTable.getDestinationForPath("/testlocationcache").toString());
756+
assertEquals("2->/anothertestlocationcache",
757+
mountTable.getDestinationForPath("/anothertestlocationcache")
758+
.toString());
759+
760+
assertEquals(2, mountTable.getLocCacheMiss().intValue());
761+
assertEquals("1->/testlocationcache",
762+
mountTable.getDestinationForPath("/testlocationcache").toString());
763+
assertEquals(3, mountTable.getLocCacheAccess().intValue());
764+
765+
// Cleanup before exit
766+
mountTable.removeEntry("/testlocationcache");
767+
mountTable.removeEntry("/anothertestlocationcache");
768+
}
769+
}

0 commit comments

Comments
 (0)