Skip to content

Commit 6a1fd44

Browse files
committed
HBASE-27795: Define RPC API for cache cleaning
1 parent 7dd4d0c commit 6a1fd44

File tree

18 files changed

+346
-0
lines changed

18 files changed

+346
-0
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,4 +2651,13 @@ List<LogEntry> getLogEntries(Set<ServerName> serverNames, String logType, Server
26512651
* Get the list of cached files
26522652
*/
26532653
List<String> getCachedFilesList(ServerName serverName) throws IOException;
2654+
2655+
/**
2656+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
2657+
* the RegionServer.
2658+
* @param serverName ServerName
2659+
* @return A map of filename and number of blocks evicted.
2660+
* @throws IOException if a remote or network exception occurs
2661+
*/
2662+
Map<String, Integer> uncacheStaleBlocks(ServerName serverName) throws IOException;
26542663
}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,4 +1136,9 @@ public void flushMasterStore() throws IOException {
11361136
public List<String> getCachedFilesList(ServerName serverName) throws IOException {
11371137
return get(admin.getCachedFilesList(serverName));
11381138
}
1139+
1140+
@Override
1141+
public Map<String, Integer> uncacheStaleBlocks(ServerName serverName) throws IOException {
1142+
return get(admin.uncacheStaleBlocks(serverName));
1143+
}
11391144
}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,4 +1861,12 @@ CompletableFuture<List<LogEntry>> getLogEntries(Set<ServerName> serverNames, Str
18611861
* Get the list of cached files
18621862
*/
18631863
CompletableFuture<List<String>> getCachedFilesList(ServerName serverName);
1864+
1865+
/**
1866+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
1867+
* the RegionServer.
1868+
* @param serverName ServerName
1869+
* @return A map of filename and number of blocks evicted.
1870+
*/
1871+
CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName);
18641872
}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,4 +1005,9 @@ public CompletableFuture<Void> flushMasterStore() {
10051005
public CompletableFuture<List<String>> getCachedFilesList(ServerName serverName) {
10061006
return wrap(rawAdmin.getCachedFilesList(serverName));
10071007
}
1008+
1009+
@Override
1010+
public CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName) {
1011+
return wrap(rawAdmin.uncacheStaleBlocks(serverName));
1012+
}
10081013
}

hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
145145
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
146146
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
147+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
148+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
147149
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationRequest;
148150
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationResponse;
149151
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
@@ -4537,4 +4539,15 @@ List<String>> adminCall(controller, stub, request.build(),
45374539
resp -> resp.getCachedFilesList()))
45384540
.serverName(serverName).call();
45394541
}
4542+
4543+
@Override
4544+
public CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName) {
4545+
UncacheStaleBlocksRequest.Builder request = UncacheStaleBlocksRequest.newBuilder();
4546+
return this.<Map<String, Integer>> newAdminCaller()
4547+
.action((controller, stub) -> this.<UncacheStaleBlocksRequest, UncacheStaleBlocksResponse,
4548+
Map<String, Integer>> adminCall(controller, stub, request.build(),
4549+
(s, c, req, done) -> s.uncacheStaleBlocks(c, req, done),
4550+
resp -> resp.getUncachedFilesMap()))
4551+
.serverName(serverName).call();
4552+
}
45404553
}

hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@
166166
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetStoreFileResponse;
167167
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.OpenRegionRequest;
168168
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ServerInfo;
169+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
170+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
169171
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionRequest;
170172
import org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos;
171173
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
@@ -1835,6 +1837,22 @@ public static List<String> getCachedFilesList(final RpcController controller,
18351837
return new ArrayList<>(response.getCachedFilesList());
18361838
}
18371839

1840+
/**
1841+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
1842+
* the RegionServer.
1843+
*/
1844+
public static Map<String, Integer> uncacheStaleBlocks(final RpcController controller,
1845+
final AdminService.BlockingInterface admin) throws IOException {
1846+
UncacheStaleBlocksRequest request = UncacheStaleBlocksRequest.newBuilder().build();
1847+
UncacheStaleBlocksResponse response = null;
1848+
try {
1849+
response = admin.uncacheStaleBlocks(controller, request);
1850+
} catch (ServiceException se) {
1851+
throw getRemoteException(se);
1852+
}
1853+
return response.getUncachedFilesMap();
1854+
}
1855+
18381856
/**
18391857
* Get the list of region info from a GetOnlineRegionResponse
18401858
* @param proto the GetOnlineRegionResponse

hbase-protocol-shaded/src/main/protobuf/server/region/Admin.proto

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ message ClearSlowLogResponses {
336336
required bool is_cleaned = 1;
337337
}
338338

339+
message UncacheStaleBlocksRequest {
340+
}
341+
342+
message UncacheStaleBlocksResponse {
343+
map<string, int32> uncached_files = 1;
344+
}
345+
346+
339347
service AdminService {
340348
rpc GetRegionInfo(GetRegionInfoRequest)
341349
returns(GetRegionInfoResponse);
@@ -415,4 +423,7 @@ service AdminService {
415423
rpc GetCachedFilesList(GetCachedFilesListRequest)
416424
returns(GetCachedFilesListResponse);
417425

426+
rpc UncacheStaleBlocks(UncacheStaleBlocksRequest)
427+
returns(UncacheStaleBlocksResponse);
428+
418429
}

hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.Optional;
2323
import org.apache.hadoop.fs.Path;
24+
import org.apache.hadoop.hbase.regionserver.RegionAvailabilityChecker;
2425
import org.apache.hadoop.hbase.util.Pair;
2526
import org.apache.yetus.audience.InterfaceAudience;
2627

@@ -245,4 +246,15 @@ default Optional<Integer> getBlockSize(BlockCacheKey key) {
245246
default Optional<Map<String, Pair<String, Long>>> getFullyCachedFiles() {
246247
return Optional.empty();
247248
}
249+
250+
/**
251+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
252+
* the RegionServer.
253+
* @param regionAvailabilityChecker RegionAvailabilityChecker
254+
* @return A map of filename and number of blocks evicted.
255+
*/
256+
default Optional<Map<String, Integer>>
257+
uncacheStaleBlocks(RegionAvailabilityChecker regionAvailabilityChecker) {
258+
return Optional.empty();
259+
}
248260
}

hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase.io.hfile;
1919

20+
import java.util.HashMap;
2021
import java.util.Iterator;
2122
import java.util.Map;
2223
import java.util.Optional;
@@ -25,6 +26,7 @@
2526
import org.apache.hadoop.fs.Path;
2627
import org.apache.hadoop.hbase.io.HeapSize;
2728
import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache;
29+
import org.apache.hadoop.hbase.regionserver.RegionAvailabilityChecker;
2830
import org.apache.hadoop.hbase.util.Pair;
2931
import org.apache.yetus.audience.InterfaceAudience;
3032
import org.slf4j.Logger;
@@ -439,6 +441,16 @@ public Optional<Map<String, Pair<String, Long>>> getFullyCachedFiles() {
439441
return this.l2Cache.getFullyCachedFiles();
440442
}
441443

444+
@Override
445+
public Optional<Map<String, Integer>>
446+
uncacheStaleBlocks(RegionAvailabilityChecker regionAvailabilityChecker) {
447+
Map<String, Integer> uncachedStaleBlocksMap =
448+
l1Cache.uncacheStaleBlocks(regionAvailabilityChecker).orElseGet(HashMap::new);
449+
l2Cache.uncacheStaleBlocks(regionAvailabilityChecker).ifPresent(
450+
map2 -> map2.forEach((key, value) -> uncachedStaleBlocksMap.merge(key, value, Integer::sum)));
451+
return Optional.of(uncachedStaleBlocksMap);
452+
}
453+
442454
@Override
443455
public void setMaxSize(long size) {
444456
this.l1Cache.setMaxSize(size);

hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collections;
2929
import java.util.Comparator;
30+
import java.util.HashMap;
3031
import java.util.HashSet;
3132
import java.util.Iterator;
3233
import java.util.List;
@@ -76,6 +77,7 @@
7677
import org.apache.hadoop.hbase.nio.ByteBuff;
7778
import org.apache.hadoop.hbase.nio.RefCnt;
7879
import org.apache.hadoop.hbase.protobuf.ProtobufMagic;
80+
import org.apache.hadoop.hbase.regionserver.RegionAvailabilityChecker;
7981
import org.apache.hadoop.hbase.util.Bytes;
8082
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
8183
import org.apache.hadoop.hbase.util.IdReadWriteLock;
@@ -2144,4 +2146,21 @@ public Optional<Integer> getBlockSize(BlockCacheKey key) {
21442146
}
21452147

21462148
}
2149+
2150+
public Optional<Map<String, Integer>>
2151+
uncacheStaleBlocks(RegionAvailabilityChecker regionAvailabilityChecker) {
2152+
Map<String, Integer> evictedFilesWithStaleBlocks = new HashMap<>();
2153+
2154+
fullyCachedFiles.forEach((fileName, value) -> {
2155+
int blocksEvicted = (!regionAvailabilityChecker.isRegionServedByTheServer(value.getFirst()))
2156+
? evictBlocksByHfileName(fileName)
2157+
: 0;
2158+
evictedFilesWithStaleBlocks.put(fileName, blocksEvicted);
2159+
LOG.debug(
2160+
"Uncached {} blocks belonging to the file {} as the region {} "
2161+
+ "is not served by the region server anymore.",
2162+
blocksEvicted, fileName, value.getFirst());
2163+
});
2164+
return Optional.of(evictedFilesWithStaleBlocks);
2165+
}
21472166
}

0 commit comments

Comments
 (0)