Skip to content

Commit f721c7c

Browse files
committed
HBASE-27795: Define RPC API for cache cleaning
1 parent 69d980a commit f721c7c

File tree

16 files changed

+316
-0
lines changed

16 files changed

+316
-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
@@ -2615,4 +2615,13 @@ List<LogEntry> getLogEntries(Set<ServerName> serverNames, String logType, Server
26152615
* Flush master local region
26162616
*/
26172617
void flushMasterStore() throws IOException;
2618+
2619+
/**
2620+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
2621+
* the RegionServer.
2622+
* @param serverName ServerName
2623+
* @return A map of filename and number of blocks evicted.
2624+
* @throws IOException if a remote or network exception occurs
2625+
*/
2626+
Map<String, Integer> uncacheStaleBlocks(ServerName serverName) throws IOException;
26182627
}

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
@@ -1115,4 +1115,9 @@ public List<LogEntry> getLogEntries(Set<ServerName> serverNames, String logType,
11151115
public void flushMasterStore() throws IOException {
11161116
get(admin.flushMasterStore());
11171117
}
1118+
1119+
@Override
1120+
public Map<String, Integer> uncacheStaleBlocks(ServerName serverName) throws IOException {
1121+
return get(admin.uncacheStaleBlocks(serverName));
1122+
}
11181123
}

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
@@ -1837,4 +1837,12 @@ CompletableFuture<List<LogEntry>> getLogEntries(Set<ServerName> serverNames, Str
18371837
* Flush master local region
18381838
*/
18391839
CompletableFuture<Void> flushMasterStore();
1840+
1841+
/**
1842+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
1843+
* the RegionServer.
1844+
* @param serverName ServerName
1845+
* @return A map of filename and number of blocks evicted.
1846+
*/
1847+
CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName);
18401848
}

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
@@ -990,4 +990,9 @@ public CompletableFuture<List<LogEntry>> getLogEntries(Set<ServerName> serverNam
990990
public CompletableFuture<Void> flushMasterStore() {
991991
return wrap(rawAdmin.flushMasterStore());
992992
}
993+
994+
@Override
995+
public CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName) {
996+
return wrap(rawAdmin.uncacheStaleBlocks(serverName));
997+
}
993998
}

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
@@ -142,6 +142,8 @@
142142
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
143143
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
144144
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
145+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
146+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
145147
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationRequest;
146148
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateConfigurationResponse;
147149
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
@@ -4453,4 +4455,15 @@ Void> call(controller, stub, request.build(),
44534455
(s, c, req, done) -> s.flushMasterStore(c, req, done), resp -> null))
44544456
.call();
44554457
}
4458+
4459+
@Override
4460+
public CompletableFuture<Map<String, Integer>> uncacheStaleBlocks(ServerName serverName) {
4461+
UncacheStaleBlocksRequest.Builder request = UncacheStaleBlocksRequest.newBuilder();
4462+
return this.<Map<String, Integer>> newAdminCaller()
4463+
.action((controller, stub) -> this.<UncacheStaleBlocksRequest, UncacheStaleBlocksResponse,
4464+
Map<String, Integer>> adminCall(controller, stub, request.build(),
4465+
(s, c, req, done) -> s.uncacheStaleBlocks(c, req, done),
4466+
resp -> resp.getUncachedFilesMap()))
4467+
.serverName(serverName).call();
4468+
}
44564469
}

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
@@ -163,6 +163,8 @@
163163
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetStoreFileResponse;
164164
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.OpenRegionRequest;
165165
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.ServerInfo;
166+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
167+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
166168
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionRequest;
167169
import org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos;
168170
import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
@@ -3767,4 +3769,20 @@ public static <T extends Message> T parseDelimitedFrom(InputStream in, Parser<T>
37673769
return parser.parseFrom(bytes);
37683770
}
37693771
}
3772+
3773+
/**
3774+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
3775+
* the RegionServer.
3776+
*/
3777+
public static Map<String, Integer> uncacheStaleBlocks(final RpcController controller,
3778+
final AdminService.BlockingInterface admin) throws IOException {
3779+
UncacheStaleBlocksRequest request = UncacheStaleBlocksRequest.newBuilder().build();
3780+
UncacheStaleBlocksResponse response = null;
3781+
try {
3782+
response = admin.uncacheStaleBlocks(controller, request);
3783+
} catch (ServiceException se) {
3784+
throw getRemoteException(se);
3785+
}
3786+
return response.getUncachedFilesMap();
3787+
}
37703788
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ message ClearSlowLogResponses {
329329
required bool is_cleaned = 1;
330330
}
331331

332+
message UncacheStaleBlocksRequest {
333+
}
334+
335+
message UncacheStaleBlocksResponse {
336+
map<string, int32> uncached_files = 1;
337+
}
338+
332339
service AdminService {
333340
rpc GetRegionInfo(GetRegionInfoRequest)
334341
returns(GetRegionInfoResponse);
@@ -405,4 +412,7 @@ service AdminService {
405412
rpc GetLogEntries(LogRequest)
406413
returns(LogEntry);
407414

415+
rpc UncacheStaleBlocks(UncacheStaleBlocksRequest)
416+
returns(UncacheStaleBlocksResponse);
417+
408418
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package org.apache.hadoop.hbase.io.hfile;
1919

2020
import java.util.Iterator;
21+
import java.util.Map;
22+
import java.util.Optional;
23+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
2124
import org.apache.yetus.audience.InterfaceAudience;
2225

2326
/**
@@ -161,4 +164,14 @@ default Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repe
161164
default boolean isMetaBlock(BlockType blockType) {
162165
return blockType != null && blockType.getCategory() != BlockType.BlockCategory.DATA;
163166
}
167+
168+
/**
169+
* Clean Cache by evicting the blocks of files belonging to regions that are no longer served by
170+
* the RegionServer.
171+
* @param server HRegionServer
172+
* @return A map of filename and number of blocks evicted.
173+
*/
174+
default Optional<Map<String, Integer>> uncacheStaleBlocks(HRegionServer server) {
175+
return Optional.empty();
176+
}
164177
}

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

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

20+
import java.util.HashMap;
2021
import java.util.Iterator;
22+
import java.util.Map;
23+
import java.util.Optional;
2124
import org.apache.hadoop.hbase.io.HeapSize;
2225
import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache;
26+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
2327
import org.apache.yetus.audience.InterfaceAudience;
2428

2529
/**
@@ -400,4 +404,13 @@ public FirstLevelBlockCache getFirstLevelCache() {
400404
public BlockCache getSecondLevelCache() {
401405
return l2Cache;
402406
}
407+
408+
@Override
409+
public Optional<Map<String, Integer>> uncacheStaleBlocks(HRegionServer server) {
410+
Map<String, Integer> uncachedStaleBlocksMap =
411+
l1Cache.uncacheStaleBlocks(server).orElseGet(HashMap::new);
412+
l2Cache.uncacheStaleBlocks(server).ifPresent(
413+
map2 -> map2.forEach((key, value) -> uncachedStaleBlocksMap.merge(key, value, Integer::sum)));
414+
return Optional.of(uncachedStaleBlocksMap);
415+
}
403416
}

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

Lines changed: 26 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;
@@ -55,6 +56,7 @@
5556
import org.apache.hadoop.fs.Path;
5657
import org.apache.hadoop.hbase.HBaseConfiguration;
5758
import org.apache.hadoop.hbase.HBaseIOException;
59+
import org.apache.hadoop.hbase.NotServingRegionException;
5860
import org.apache.hadoop.hbase.TableName;
5961
import org.apache.hadoop.hbase.client.Admin;
6062
import org.apache.hadoop.hbase.io.ByteBuffAllocator;
@@ -75,6 +77,7 @@
7577
import org.apache.hadoop.hbase.nio.ByteBuff;
7678
import org.apache.hadoop.hbase.nio.RefCnt;
7779
import org.apache.hadoop.hbase.protobuf.ProtobufMagic;
80+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
7881
import org.apache.hadoop.hbase.util.Bytes;
7982
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
8083
import org.apache.hadoop.hbase.util.IdReadWriteLock;
@@ -2002,4 +2005,27 @@ public void fileCacheCompleted(Path filePath, long size) {
20022005
regionCachedSizeMap.merge(regionName, size, (oldpf, fileSize) -> oldpf + fileSize);
20032006
}
20042007

2008+
@Override
2009+
public Optional<Map<String, Integer>> uncacheStaleBlocks(HRegionServer server) {
2010+
Map<String, Integer> evictedFilesWithStaleBlocks = new HashMap<>();
2011+
2012+
fullyCachedFiles.forEach((fileName, value) -> {
2013+
int blocksEvicted;
2014+
try {
2015+
if (!server.getRegionByEncodedName(value.getFirst()).isAvailable()) {
2016+
blocksEvicted = evictBlocksByHfileName(fileName);
2017+
} else {
2018+
blocksEvicted = 0;
2019+
}
2020+
} catch (NotServingRegionException nsre) {
2021+
LOG.debug(
2022+
"Evicting blocks for file {} as the region {} is not served by the Region Server {} anymore.",
2023+
fileName, fullyCachedFiles.get(fileName).getFirst(),
2024+
server.getServerName().getServerName());
2025+
blocksEvicted = evictBlocksByHfileName(fileName);
2026+
}
2027+
evictedFilesWithStaleBlocks.put(fileName, blocksEvicted);
2028+
});
2029+
return Optional.of(evictedFilesWithStaleBlocks);
2030+
}
20052031
}

hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@
182182
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
183183
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
184184
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
185+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
186+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
185187
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
186188
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesResponse;
187189
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionRequest;
@@ -3609,4 +3611,10 @@ public FlushTableResponse flushTable(RpcController controller, FlushTableRequest
36093611
throw new ServiceException(ioe);
36103612
}
36113613
}
3614+
3615+
@Override
3616+
public UncacheStaleBlocksResponse uncacheStaleBlocks(RpcController controller,
3617+
UncacheStaleBlocksRequest request) throws ServiceException {
3618+
throw new ServiceException(new DoNotRetryIOException("Unsupported method on master"));
3619+
}
36123620
}

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@
190190
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
191191
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
192192
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
193+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
194+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
193195
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
194196
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesResponse;
195197
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WALEntry;
@@ -3933,4 +3935,15 @@ public void onConfigurationChange(Configuration conf) {
39333935
super.onConfigurationChange(conf);
39343936
setReloadableGuardrails(conf);
39353937
}
3938+
3939+
@Override
3940+
public UncacheStaleBlocksResponse uncacheStaleBlocks(RpcController controller,
3941+
UncacheStaleBlocksRequest request) throws ServiceException {
3942+
UncacheStaleBlocksResponse.Builder responseBuilder = UncacheStaleBlocksResponse.newBuilder();
3943+
Map<String, Integer> evictedFilesWithStaleBlocks = new HashMap<>();
3944+
server.getBlockCache().flatMap(bc -> bc.uncacheStaleBlocks(server))
3945+
.ifPresent(evictedFilesWithStaleBlocks::putAll);
3946+
responseBuilder.putAllUncachedFiles(evictedFilesWithStaleBlocks);
3947+
return responseBuilder.build();
3948+
}
39363949
}

0 commit comments

Comments
 (0)