Skip to content

Commit 724a4b9

Browse files
committed
HBASE-27795: Define RPC API for cache cleaning
1 parent 256f10b commit 724a4b9

File tree

15 files changed

+316
-1
lines changed

15 files changed

+316
-1
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/bucket/BucketCache.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,5 @@ public Optional<Integer> getBlockSize(BlockCacheKey key) {
21462146
} else {
21472147
return Optional.of(entry.getOnDiskSizeWithHeader());
21482148
}
2149-
21502149
}
21512150
}

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
@@ -184,6 +184,8 @@
184184
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
185185
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
186186
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
187+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
188+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
187189
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
188190
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesResponse;
189191
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WarmupRegionRequest;
@@ -3402,6 +3404,12 @@ public HBaseProtos.LogEntry getLogEntries(RpcController controller,
34023404
throw new ServiceException("Invalid request params");
34033405
}
34043406

3407+
@Override
3408+
public UncacheStaleBlocksResponse uncacheStaleBlocks(RpcController controller,
3409+
UncacheStaleBlocksRequest request) throws ServiceException {
3410+
throw new ServiceException(new DoNotRetryIOException("Unsupported method on master"));
3411+
}
3412+
34053413
private MasterProtos.BalancerDecisionsResponse
34063414
getBalancerDecisions(MasterProtos.BalancerDecisionsRequest request) {
34073415
final NamedQueueRecorder namedQueueRecorder = this.server.getNamedQueueRecorder();

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@
5858
import java.util.TimerTask;
5959
import java.util.TreeMap;
6060
import java.util.TreeSet;
61+
import java.util.concurrent.Callable;
6162
import java.util.concurrent.ConcurrentHashMap;
6263
import java.util.concurrent.ConcurrentMap;
6364
import java.util.concurrent.ConcurrentSkipListMap;
65+
import java.util.concurrent.ExecutorService;
66+
import java.util.concurrent.Executors;
6467
import java.util.concurrent.ThreadLocalRandom;
6568
import java.util.concurrent.TimeUnit;
6669
import java.util.concurrent.atomic.AtomicBoolean;
@@ -1659,6 +1662,43 @@ public RegionLoad createRegionLoad(final String encodedRegionName) throws IOExce
16591662
return r != null ? createRegionLoad(r, null, null) : null;
16601663
}
16611664

1665+
public Map<String, Integer> uncacheStaleBlocks() {
1666+
Map<String, Pair<String, Long>> fullyCachedFiles =
1667+
this.getBlockCache().flatMap(BlockCache::getFullyCachedFiles).orElse(Collections.emptyMap());
1668+
Map<String, Integer> evictedFilesWithStaleBlocks = new ConcurrentHashMap<>();
1669+
1670+
ExecutorService executor = Executors.newFixedThreadPool(6);
1671+
1672+
List<Callable<Void>> tasks = new ArrayList<>();
1673+
1674+
fullyCachedFiles.forEach((fileName, value) -> {
1675+
Callable<Void> task = () -> {
1676+
HRegion regionOnServer = getRegion(value.getFirst());
1677+
int blocksEvicted = (regionOnServer == null || !regionOnServer.isAvailable())
1678+
? this.getBlockCache().get().evictBlocksByHfileName(fileName)
1679+
: 0;
1680+
evictedFilesWithStaleBlocks.put(fileName, blocksEvicted);
1681+
LOG.info(
1682+
"Uncached {} blocks belonging to the file {} as the region {} "
1683+
+ "is not served by the region server {} anymore.",
1684+
blocksEvicted, fileName, value.getFirst(), this.getServerName());
1685+
return null;
1686+
};
1687+
tasks.add(task);
1688+
});
1689+
1690+
try {
1691+
executor.invokeAll(tasks);
1692+
} catch (InterruptedException e) {
1693+
Thread.currentThread().interrupt();
1694+
LOG.error("Thread interrupted while processing tasks for uncaching stale blocks: {}",
1695+
e.getMessage());
1696+
} finally {
1697+
executor.shutdown();
1698+
}
1699+
return evictedFilesWithStaleBlocks;
1700+
}
1701+
16621702
/**
16631703
* Inner class that runs on a long period checking if regions need compaction.
16641704
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@
192192
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.RollWALWriterResponse;
193193
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerRequest;
194194
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.StopServerResponse;
195+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksRequest;
196+
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UncacheStaleBlocksResponse;
195197
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
196198
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.UpdateFavoredNodesResponse;
197199
import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.WALEntry;
@@ -3964,4 +3966,13 @@ public GetCachedFilesListResponse getCachedFilesList(RpcController controller,
39643966
});
39653967
return responseBuilder.addAllCachedFiles(fullyCachedFiles).build();
39663968
}
3969+
3970+
@Override
3971+
public UncacheStaleBlocksResponse uncacheStaleBlocks(RpcController controller,
3972+
UncacheStaleBlocksRequest request) throws ServiceException {
3973+
UncacheStaleBlocksResponse.Builder responseBuilder = UncacheStaleBlocksResponse.newBuilder();
3974+
Map<String, Integer> evictedFilesWithStaleBlocks = new HashMap<>(server.uncacheStaleBlocks());
3975+
responseBuilder.putAllUncachedFiles(evictedFilesWithStaleBlocks);
3976+
return responseBuilder.build();
3977+
}
39673978
}

0 commit comments

Comments
 (0)