@@ -1082,7 +1082,7 @@ public void setConf(Configuration conf) {
1082
1082
* and its encoding vs. {@code expectedDataBlockEncoding}. Unpacks the block as necessary.
1083
1083
*/
1084
1084
private HFileBlock getCachedBlock (BlockCacheKey cacheKey , boolean cacheBlock , boolean useLock ,
1085
- boolean isCompaction , boolean updateCacheMetrics , BlockType expectedBlockType ,
1085
+ boolean updateCacheMetrics , BlockType expectedBlockType ,
1086
1086
DataBlockEncoding expectedDataBlockEncoding ) throws IOException {
1087
1087
// Check cache for block. If found return.
1088
1088
BlockCache cache = cacheConf .getBlockCache ().orElse (null );
@@ -1187,7 +1187,7 @@ public HFileBlock getMetaBlock(String metaBlockName, boolean cacheBlock) throws
1187
1187
1188
1188
cacheBlock &= cacheConf .shouldCacheBlockOnRead (BlockType .META .getCategory ());
1189
1189
HFileBlock cachedBlock =
1190
- getCachedBlock (cacheKey , cacheBlock , false , true , true , BlockType .META , null );
1190
+ getCachedBlock (cacheKey , cacheBlock , false , true , BlockType .META , null );
1191
1191
if (cachedBlock != null ) {
1192
1192
assert cachedBlock .isUnpacked () : "Packed block leak." ;
1193
1193
// Return a distinct 'shallow copy' of the block,
@@ -1234,6 +1234,15 @@ private boolean shouldUseHeap(BlockType expectedBlockType) {
1234
1234
public HFileBlock readBlock (long dataBlockOffset , long onDiskBlockSize , final boolean cacheBlock ,
1235
1235
boolean pread , final boolean isCompaction , boolean updateCacheMetrics ,
1236
1236
BlockType expectedBlockType , DataBlockEncoding expectedDataBlockEncoding ) throws IOException {
1237
+ return readBlock (dataBlockOffset , onDiskBlockSize , cacheBlock , pread , isCompaction ,
1238
+ updateCacheMetrics , expectedBlockType , expectedDataBlockEncoding , false );
1239
+ }
1240
+
1241
+ @ Override
1242
+ public HFileBlock readBlock (long dataBlockOffset , long onDiskBlockSize , final boolean cacheBlock ,
1243
+ boolean pread , final boolean isCompaction , boolean updateCacheMetrics ,
1244
+ BlockType expectedBlockType , DataBlockEncoding expectedDataBlockEncoding , boolean cacheOnly )
1245
+ throws IOException {
1237
1246
if (dataBlockIndexReader == null ) {
1238
1247
throw new IOException (path + " block index not loaded" );
1239
1248
}
@@ -1257,17 +1266,18 @@ public HFileBlock readBlock(long dataBlockOffset, long onDiskBlockSize, final bo
1257
1266
try (TraceScope traceScope = TraceUtil .createTrace ("HFileReaderImpl.readBlock" )) {
1258
1267
while (true ) {
1259
1268
// Check cache for block. If found return.
1260
- if (cacheConf .shouldReadBlockFromCache (expectedBlockType )) {
1269
+ if (cacheConf .shouldReadBlockFromCache (expectedBlockType ) && ! cacheOnly ) {
1261
1270
if (useLock ) {
1262
1271
lockEntry = offsetLock .getLockEntry (dataBlockOffset );
1263
1272
}
1264
1273
// Try and get the block from the block cache. If the useLock variable is true then this
1265
1274
// is the second time through the loop and it should not be counted as a block cache miss.
1266
- HFileBlock cachedBlock = getCachedBlock (cacheKey , cacheBlock , useLock , isCompaction ,
1267
- updateCacheMetrics , expectedBlockType , expectedDataBlockEncoding );
1275
+ HFileBlock cachedBlock = getCachedBlock (cacheKey , cacheBlock , useLock , updateCacheMetrics ,
1276
+ expectedBlockType , expectedDataBlockEncoding );
1268
1277
if (cachedBlock != null ) {
1269
1278
if (LOG .isTraceEnabled ()) {
1270
- LOG .trace ("From Cache " + cachedBlock );
1279
+ LOG .trace ("Block for file {} is coming from Cache {}" ,
1280
+ Bytes .toString (cachedBlock .getHFileContext ().getTableName ()), cachedBlock );
1271
1281
}
1272
1282
TraceUtil .addTimelineAnnotation ("blockCacheHit" );
1273
1283
assert cachedBlock .isUnpacked () : "Packed block leak." ;
@@ -1304,14 +1314,30 @@ public HFileBlock readBlock(long dataBlockOffset, long onDiskBlockSize, final bo
1304
1314
HFileBlock hfileBlock = fsBlockReader .readBlockData (dataBlockOffset , onDiskBlockSize , pread ,
1305
1315
!isCompaction , shouldUseHeap (expectedBlockType ));
1306
1316
validateBlockType (hfileBlock , expectedBlockType );
1307
- HFileBlock unpacked = hfileBlock .unpack (hfileContext , fsBlockReader );
1308
1317
BlockType .BlockCategory category = hfileBlock .getBlockType ().getCategory ();
1318
+ final boolean cacheCompressed = cacheConf .shouldCacheCompressed (category );
1319
+ final boolean cacheOnRead = cacheConf .shouldCacheBlockOnRead (category );
1320
+
1321
+ // Don't need the unpacked block back and we're storing the block in the cache compressed
1322
+ if (cacheOnly && cacheCompressed && cacheOnRead ) {
1323
+ LOG .debug ("Skipping decompression of block in prefetch" );
1324
+ // Cache the block if necessary
1325
+ cacheConf .getBlockCache ().ifPresent (cache -> {
1326
+ if (cacheBlock && cacheConf .shouldCacheBlockOnRead (category )) {
1327
+ cache .cacheBlock (cacheKey , hfileBlock , cacheConf .isInMemory ());
1328
+ }
1329
+ });
1309
1330
1331
+ if (updateCacheMetrics && hfileBlock .getBlockType ().isData ()) {
1332
+ HFile .DATABLOCK_READ_COUNT .increment ();
1333
+ }
1334
+ return hfileBlock ;
1335
+ }
1336
+ HFileBlock unpacked = hfileBlock .unpack (hfileContext , fsBlockReader );
1310
1337
// Cache the block if necessary
1311
1338
cacheConf .getBlockCache ().ifPresent (cache -> {
1312
1339
if (cacheBlock && cacheConf .shouldCacheBlockOnRead (category )) {
1313
- cache .cacheBlock (cacheKey ,
1314
- cacheConf .shouldCacheCompressed (category ) ? hfileBlock : unpacked ,
1340
+ cache .cacheBlock (cacheKey , cacheCompressed ? hfileBlock : unpacked ,
1315
1341
cacheConf .isInMemory ());
1316
1342
}
1317
1343
});
0 commit comments