wchevreuil commented on code in PR #7398: URL: https://github.com/apache/hbase/pull/7398#discussion_r2788302435
########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RowCacheService.java: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Scan; + +/** + * It is responsible for populating the row cache and retrieving rows from it. + */ [email protected] +public class RowCacheService { + private final boolean enabledByConf; + private final RowCache rowCache; + + @FunctionalInterface + interface RowOperation<R> { + R execute() throws IOException; + } + + <R> R execute(RowOperation<R> operation) throws IOException { + return operation.execute(); + } + + RowCacheService(Configuration conf) { + enabledByConf = + conf.getFloat(HConstants.ROW_CACHE_SIZE_KEY, HConstants.ROW_CACHE_SIZE_DEFAULT) > 0; + // TODO: implement row cache + rowCache = null; + } + + <R> R mutateWithRowCacheBarrier(HRegion region, byte[] row, RowOperation<R> operation) + throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + RowCacheKey key = new RowCacheKey(region, row); + // TODO: implement mutate with row cache barrier logic + evictRow(key); + return execute(operation); + } + + <R> R mutateWithRowCacheBarrier(HRegion region, List<Mutation> mutations, + RowOperation<R> operation) throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + // TODO: implement mutate with row cache barrier logic + Set<RowCacheKey> rowCacheKeys = new HashSet<>(mutations.size()); + mutations.forEach(mutation -> rowCacheKeys.add(new RowCacheKey(region, mutation.getRow()))); + rowCacheKeys.forEach(this::evictRow); + + return execute(operation); + } + + void evictRow(RowCacheKey key) { + rowCache.evictRow(key); + } + + boolean canCacheRow(Get get, Region region) { + // TODO: implement logic to determine if the row can be cached + return false; + } + + RegionScannerImpl getScanner(HRegion region, Get get, Scan scan, List<Cell> results) + throws IOException { + if (!canCacheRow(get, region)) { + return getScannerInternal(region, scan, results); + } + + RowCacheKey key = new RowCacheKey(region, get.getRow()); + + // Try get from row cache + if (tryGetFromCache(region, key, get, results)) { + // Cache is hit, and then no scanner is created + return null; + } + + RegionScannerImpl scanner = getScannerInternal(region, scan, results); + populateCache(region, results, key); + return scanner; + } Review Comment: I think RowCacheService shouldn't bother about RegionScannerImpl, but rather only the cache itself, so it should only check for the rowkey in the cache and populate the results accordingly. It's then up for the HRegion to create the scanner when no results are found in the row cache. So we could make this method void, or return List<Cell> with the found results, and get rid of the scanner creation logic. These would be done in HRegion.getScannerResults. ########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RowCacheService.java: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Scan; + +/** + * It is responsible for populating the row cache and retrieving rows from it. + */ [email protected] +public class RowCacheService { + private final boolean enabledByConf; + private final RowCache rowCache; + + @FunctionalInterface + interface RowOperation<R> { + R execute() throws IOException; + } + + <R> R execute(RowOperation<R> operation) throws IOException { + return operation.execute(); + } + + RowCacheService(Configuration conf) { + enabledByConf = + conf.getFloat(HConstants.ROW_CACHE_SIZE_KEY, HConstants.ROW_CACHE_SIZE_DEFAULT) > 0; + // TODO: implement row cache + rowCache = null; + } + + <R> R mutateWithRowCacheBarrier(HRegion region, byte[] row, RowOperation<R> operation) + throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + RowCacheKey key = new RowCacheKey(region, row); + // TODO: implement mutate with row cache barrier logic + evictRow(key); + return execute(operation); + } + + <R> R mutateWithRowCacheBarrier(HRegion region, List<Mutation> mutations, + RowOperation<R> operation) throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + // TODO: implement mutate with row cache barrier logic + Set<RowCacheKey> rowCacheKeys = new HashSet<>(mutations.size()); + mutations.forEach(mutation -> rowCacheKeys.add(new RowCacheKey(region, mutation.getRow()))); + rowCacheKeys.forEach(this::evictRow); + + return execute(operation); + } + + void evictRow(RowCacheKey key) { + rowCache.evictRow(key); + } + + boolean canCacheRow(Get get, Region region) { + // TODO: implement logic to determine if the row can be cached + return false; + } + + RegionScannerImpl getScanner(HRegion region, Get get, Scan scan, List<Cell> results) + throws IOException { + if (!canCacheRow(get, region)) { + return getScannerInternal(region, scan, results); + } + + RowCacheKey key = new RowCacheKey(region, get.getRow()); + + // Try get from row cache + if (tryGetFromCache(region, key, get, results)) { + // Cache is hit, and then no scanner is created + return null; + } + + RegionScannerImpl scanner = getScannerInternal(region, scan, results); + populateCache(region, results, key); + return scanner; + } + + private boolean tryGetFromCache(HRegion region, RowCacheKey key, Get get, List<Cell> results) { + RowCells row = rowCache.getRow(key, get.getCacheBlocks()); + + if (row == null) { + return false; + } + + results.addAll(row.getCells()); + // TODO: implement update of metrics + return true; + } + + private void populateCache(HRegion region, List<Cell> results, RowCacheKey key) { + // TODO: implement with barrier to avoid cache read during mutation + try { + rowCache.cacheRow(key, new RowCells(results)); + } catch (CloneNotSupportedException ignored) { + // Not able to cache row cells, ignore + } + } + + private RegionScannerImpl getScannerInternal(HRegion region, Scan scan, List<Cell> results) + throws IOException { + RegionScannerImpl scanner = region.getScanner(scan); + scanner.next(results); + return scanner; Review Comment: Should move to HRegion (see my previous comment above). ########## hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RowCacheService.java: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Scan; + +/** + * It is responsible for populating the row cache and retrieving rows from it. + */ [email protected] +public class RowCacheService { + private final boolean enabledByConf; + private final RowCache rowCache; + + @FunctionalInterface + interface RowOperation<R> { + R execute() throws IOException; + } + + <R> R execute(RowOperation<R> operation) throws IOException { + return operation.execute(); + } + + RowCacheService(Configuration conf) { + enabledByConf = + conf.getFloat(HConstants.ROW_CACHE_SIZE_KEY, HConstants.ROW_CACHE_SIZE_DEFAULT) > 0; + // TODO: implement row cache + rowCache = null; + } + + <R> R mutateWithRowCacheBarrier(HRegion region, byte[] row, RowOperation<R> operation) + throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + RowCacheKey key = new RowCacheKey(region, row); + // TODO: implement mutate with row cache barrier logic + evictRow(key); + return execute(operation); + } + + <R> R mutateWithRowCacheBarrier(HRegion region, List<Mutation> mutations, + RowOperation<R> operation) throws IOException { + if (!region.isRowCacheEnabled()) { + return operation.execute(); + } + + // TODO: implement mutate with row cache barrier logic + Set<RowCacheKey> rowCacheKeys = new HashSet<>(mutations.size()); + mutations.forEach(mutation -> rowCacheKeys.add(new RowCacheKey(region, mutation.getRow()))); + rowCacheKeys.forEach(this::evictRow); + + return execute(operation); + } + + void evictRow(RowCacheKey key) { + rowCache.evictRow(key); + } + + boolean canCacheRow(Get get, Region region) { + // TODO: implement logic to determine if the row can be cached + return false; + } + + RegionScannerImpl getScanner(HRegion region, Get get, Scan scan, List<Cell> results) + throws IOException { + if (!canCacheRow(get, region)) { + return getScannerInternal(region, scan, results); + } + + RowCacheKey key = new RowCacheKey(region, get.getRow()); Review Comment: Could we use "namespace + tableame + rowkey" as the UID of a row in the row cache? We could then only pass the TableDescriptor, rather than the HRegion as this method parameter. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
